tips:xpath

This is an old revision of the document!


xpath

With chromium in developer mode right click on the node ⇒ “Copy XPath”

With lxml and python

print xpath of all elements

from lxml import html, etree

htmltext="bla bla bla"

doc=html.fromstring(htmltext)
#find_text = doc.xpath("//text()")
tree=etree.ElementTree(doc)
for e in tree.iter():
    print e.text,tree.getpath(e)
    

get element from xpath

from lxml import html

htmltext="bla bla bla"
xpath="/html/body/div/table[2]/tr[1]/td"

doc=html.fromstring(htmltext)
print doc.xpath(xpath)[0].text
  • tips/xpath.1443080741.txt.gz
  • Last modified: 2015/09/24 09:45
  • by scipio