如何匹配 XPath (lxml) 中元素的内容?

发布于 2024-08-29 12:35:44 字数 758 浏览 3 评论 0原文

我想使用 XPath 表达式通过 lxml 解析 HTML。我的问题是匹配标签的内容:

例如,给定元素,

<a href="http://something">Example</a>

我可以使用匹配 href 属性,

.//a[@href='http://something']

但给定表达式

.//a[.='Example']

甚至

.//a[contains(.,'Example')]

lxml 会抛出“无效节点谓词”异常。

我做错了什么?

编辑:

示例代码:

from lxml import etree
from cStringIO import StringIO

html = '<a href="http://something">Example</a>'
parser = etree.HTMLParser()
tree   = etree.parse(StringIO(html), parser)

print tree.find(".//a[text()='Example']").tag

预期输出为“a”。我收到“语法错误:节点谓词无效”

I want to parse HTML with lxml using XPath expressions. My problem is matching for the contents of a tag:

For example given the

<a href="http://something">Example</a>

element I can match the href attribute using

.//a[@href='http://something']

but the given the expression

.//a[.='Example']

or even

.//a[contains(.,'Example')]

lxml throws the 'invalid node predicate' exception.

What am I doing wrong?

EDIT:

Example code:

from lxml import etree
from cStringIO import StringIO

html = '<a href="http://something">Example</a>'
parser = etree.HTMLParser()
tree   = etree.parse(StringIO(html), parser)

print tree.find(".//a[text()='Example']").tag

Expected output is 'a'. I get 'SyntaxError: invalid node predicate'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2024-09-05 12:35:44

我会尝试使用:

.//a[text()='Example']

使用 xpath() 方法:

tree.xpath(".//a[text()='Example']")[0].tag

如果您想使用 iterfind()、findall()、find(), findtext(),请记住,ElementPath

lxml.etree支持简单路径
find、findall 和 的语法
ElementTree 上的 findtext 方法和
元素,从原文可知
ElementTree 库(ElementPath)。作为
lxml 特定扩展,这些
类还提供 xpath() 方法
支持表达式
完整的 XPath 语法,以及
自定义扩展函数。

I would try with:

.//a[text()='Example']

using xpath() method:

tree.xpath(".//a[text()='Example']")[0].tag

If case you would like to use iterfind(), findall(), find(), findtext(), keep in mind that advanced features like value comparison and functions are not available in ElementPath.

lxml.etree supports the simple path
syntax of the find, findall and
findtext methods on ElementTree and
Element, as known from the original
ElementTree library (ElementPath). As
an lxml specific extension, these
classes also provide an xpath() method
that supports expressions in the
complete XPath syntax, as well as
custom extension functions.

伤痕我心 2024-09-05 12:35:44

目前接受的答案存在性能缺陷。考虑使用:

xml_doc.finditer('.//element[.="text to match"]')

代替。

这在原始 xml.ElementTree 实现的查找语法文档中进行了记录: https://docs.python.org/3/library/xml.etree.elementtree.html#supported-xpath-syntax
lxml.etree 表示它的 find 方法使用相同的受限语法。

The currently accepted answer has performance drawbacks. Consider using:

xml_doc.finditer('.//element[.="text to match"]')

instead.

This is documented in the find syntax docs here for the original xml.ElementTree implementation: https://docs.python.org/3/library/xml.etree.elementtree.html#supported-xpath-syntax
and lxml.etree says it uses the same restricted syntax for its find methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文