- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试处理 XML 文件,但出现此错误:
XPathEvalError: Undefined namespace prefix
print "category =", item.xpath("./g:google_product_category")
<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")
最佳答案
xpath 方法接受一个额外的参数:命名空间
您能否尝试如下修改该行:
print "category =", item.xpath("./g:google_product_category", namespaces={'g': 'http://base.google.com/ns/1.0'})
关于Python 解析 XML 提要错误 : XPathEvalError: Undefined namespace prefix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44188237/
我正在尝试使用 lxml 解析 SVG 文档。这是我的代码: nsmap = { 'svg': 'http://www.w3.org/2000/svg', 'xlink': 'http
我在使用 Python 时遇到了一个我无法理解的错误。我已将代码简化到最低限度: response = requests.get('http://pycoders.com/archive') tree
我正在尝试学习如何抓取网页,在教程中我使用下面的代码会引发此错误: lxml.etree.XPathEvalError: Invalid predicate 我要查询的网站是(不要评判我,它是培训视频
我正在尝试在 python 中使用以下 xpath 查询 from lxml.html.soupparser import fromstring root = fromstring(inString)
我正在尝试处理 XML 文件,但出现此错误: XPathEvalError: Undefined namespace prefix 在这一行: print "category =", item.xpa
lxml 很好地支持 Unicode 元素名称,因为它们根据 XML 规范是有效的。但是在 XPath 中使用 Unicode 会产生错误: >>> import lxml.etree >>> e =
我正在尝试解析 xpath,但它给出了无效表达式错误。 应该工作的代码: x = tree.xpath("//description/caution[1]/preceding-sibling::*/n
我是一名优秀的程序员,十分优秀!