擅长:python、mysql、java
<p>首先,您实际上并没有在代码中使用<code>lxml</code>。导入<code>lxml</code>HTML解析器,但是忽略它,只使用标准库<a href="http://docs.python.org/2/library/xml.etree.elementtree.html" rel="nofollow">^{<cd3>} module</a>。在</p>
<p>其次,搜索<code>data/song</code>,但文档中没有任何<code>data</code>元素,因此不会找到匹配项。最后,但并非最不重要的是,这里有一个使用名称空间的文档。搜索元素时必须包含这些元素,或者使用<code>{*}</code>通配符搜索。在</p>
<p>以下为您查找歌曲:</p>
<pre><code>from lxml import etree
tree = etree.parse(URL) # lxml can load URLs for you
songs = tree.findall('{*}song')
for song in songs:
print song.attrib['title']
</code></pre>
<p>要使用显式命名空间,您必须将<code>{*}</code>通配符替换为完整的命名空间URL;默认命名空间可在<code>tree</code>对象的<code>.nsmap</code>命名空间dict中找到:</p>
^{pr2}$