Python解析XML源出错:XPathEvalError:未定义的命名空间前缀。

4
我正在尝试处理一个XML文件,但是出现了以下错误:
XPathEvalError: Undefined namespace prefix

在这行代码中:
print "category =", item.xpath("./g:google_product_category")

这是XML文件:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>example.net.br</title>
<link>http://www.example.net.br/</link>
<description>Data feed description.</description>
<item>
<title>
<![CDATA[
example
]]>
</title>
<link>
<![CDATA[
example
]]>
</link>
<description>
<![CDATA[
example]]>
</description>
<g:google_product_category>
<![CDATA[
example
]]>
</g:google_product_category>
...

这是我的代码:
headers = { 'User-Agent' : 'Mozilla/5.0' }
req = urllib2.Request(feed_url, None, headers)
file = urllib2.urlopen(req).read()

file = etree.fromstring(file)
for item in file.xpath('/rss/channel/item'):
    print "title =", item.xpath("./title/text()")[0]
    print "link =", item.xpath("./link/text()")[0]
    print "description =", item.xpath("./description/text()")[0]
    print "category =", item.xpath("./g:google_product_category")

如何解决这个问题?
1个回答

5
xpath方法接受一个额外的参数:namespaces。
请尝试将该行修改为以下内容:
print "category =", item.xpath("./g:google_product_category", namespaces={'g': 'http://base.google.com/ns/1.0'})

这里提供的信息来源可以在此处查看。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接