使用XML etree,可以这样做:
etree.getpath(element
我如何做同样的事情,但用HTML而不是XML?
答案 0 :(得分:4)
_ElementTree
有getpath
方法:
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'