python - How to get path of an element in lxml? -


I'm searching in an HTML document using XPath from LXML in Python. How do I get the path of a certain element? Here is an example from Ruby Notchri:

  Page Xpath ('// text ()'). Every do Textnode | Path = textnode.path for example puts the end of the path to ' / html / body / div / div [1] / div [1] / p / text () [1] <1  < P>]  'and this is the string that I want to get in Python.   

Use Elementary Object. Lxml Import Entry Root = etree.fromstring ('

  '& lt; baz & gt; data & lt; / baz & gt; & lt; / bar & gt; & lt; / foo & gt; ') Tree = etree.ElementTree (root) for root.iter (): print tree.getpath (e)  

print

  / foo / Foo / bar [1] / foo / bar [2] / af / bar [2] / falcon [1] / af / bar [2] / falcon [2]  

Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -