lxml - 如何获取HtmlElement的xpath?

时间:2014-10-10 17:11:03

标签: python html xml xpath lxml

使用XML etree,可以这样做:

etree.getpath(element

我如何做同样的事情,但用HTML而不是XML?

1 个答案:

答案 0 :(得分:4)

_ElementTreegetpath方法:

In [17]: import lxml.html as LH
In [18]: content = '<root><div id="pgbrk" ......>....Page Break....</div></root>'

In [19]: root = LH.fromstring(content)

In [20]: tree = root.getroottree()

In [21]: tree.getpath(root[0])
Out[21]: '/html/body/root/div'