相关疑难解决方法(0)

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

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

比如给出了

<a href="http://something">Example</a>
Run Code Online (Sandbox Code Playgroud)

element我可以匹配href属性

.//a[@href='http://something']
Run Code Online (Sandbox Code Playgroud)

但给定的表达

.//a[.='Example']
Run Code Online (Sandbox Code Playgroud)

甚至

.//a[contains(.,'Example')]
Run Code Online (Sandbox Code Playgroud)

lxml抛出'invalid node predicate'异常.

我究竟做错了什么?

编辑:

示例代码:

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
Run Code Online (Sandbox Code Playgroud)

预期产量为'a'.我得到'SyntaxError:无效的节点谓词'

python xpath lxml predicate

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

lxml ×1

predicate ×1

python ×1

xpath ×1