我试图从中提取作者:
<icon>
<tags>
<tag>steam</tag>
</tags>
<author>Author Name</author>
<authorwebsite>http://www.domain.com/</authorwebsite>
<license>
Creative Commons (Attribution-Noncommercial-Share Alike 3.0 Unported)
</license>
<licensewebsite>http://creativecommons.org/licenses/by-nc-sa/3.0/</licensewebsite>
<iconset>Name</iconset>
<iconsetid>slug</iconsetid>
<attribution/>
<additionalsizes>
<icon>
<id>99633</id>
<size>128</size>
<tags/>
<image>
http://url1
</image>
</icon>
<icon>
<id>99633</id>
<size>256</size>
<tags/>
<image>
http://url2
</image>
</icon>
<icon>
<id>99633</id>
<size>512</size>
<tags/>
<image>
http://url3
</image>
</icon>
</additionalsizes>
</icon>
我试过了:
name = dom.getElementsByTagName('author')
print name[0].firstChild.nodeValue
AttributeError:'NoneType'对象没有属性'nodeValue'
和
name = dom.getElementsByTagName('author')
print " ".join(t.nodeValue for t in name[0].childNodes if t.nodeType == t.TEXT_NODE)
返回空字符串。
怎么了?感谢。
答案 0 :(得分:2)
使用ElementTree,正如@Martijn Pieters所建议的那样:
from xml.etree import ElementTree
tree = ElementTree.fromstring('<icon><author>Author Name</author></icon>')
print tree.find('author').text
更多示例:http://www.doughellmann.com/PyMOTW/xml/etree/ElementTree/parse.html
答案 1 :(得分:2)
您忘记了从图标标记
中获取作者from xml.dom.minidom import parse
dom = parse('test.xml')
icon = dom.getElementsByTagName('icon')[0]
author = icon.getElementsByTagName('author')[0]
print author.firstChild.nodeValue
答案 2 :(得分:1)
使用lxml轻松搞笑:
from lxml import etree
dom=etree.fromstring(XML_DOC)
dom.xpath('/icon/author/text()')[0]
返回'作者姓名'