[英]xml.dom.minidom python issue
from xml.dom.minidom import *
resp = "<title> This is a test! </title>"
rssDoc = parseString(resp)
titles = rssDoc.getElementsByTagName('title')
moo = ""
for t in titles:
moo += t.nodeValue;
Gives the following error: 给出以下错误:
main.py, line 42, in
get moo += t.nodeValue;
TypeError: cannot concatenate 'str' and 'NoneType' objects
The <title>
node contains a text node as a subnode. <title>
节点包含一个文本节点作为子节点。 Maybe you want to iterate through the subnodes instead? 也许您想遍历子节点呢? Something like this:
像这样:
from xml.dom.minidom import *
resp = "<title> This is a test! </title>"
rssDoc = parseString(resp)
titles = rssDoc.getElementsByTagName('title')
moo = ""
for t in titles:
for child in t.childNodes:
if child.nodeType == child.TEXT_NODE:
moo += child.data
else:
moo += "not text "
print moo
For learning xml.dom.minidom you might also check out the section in Dive Into Python . 要学习xml.dom.minidom,您还可以查看Dive Into Python中的部分 。
Because is not a text node, but an element node. 因为不是文本节点,而是元素节点。 The text node which contains the " This is a test! " string is actually a child node of this element node.
包含“ This is a test!”字符串的文本节点实际上是此元素节点的子节点。
So you can try this (untested, not assumes existence of the text node): 因此,您可以尝试以下操作(未经测试,不假定存在文本节点):
if t.nodeType == t.ELEMENT_NODE:
moo += t.childNodes[0].data
因为t.nodeType
当然不等于t.TEXT_NODE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.