tips:xpath

xpath

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

With lxml and python

sudo apt-get install libxml2-dev libxslt1-dev python-dev
pip install lxml

print xpath of all elements

from lxml import html, etree

htmltext="bla bla bla"

doc=html.fromstring(htmltext)
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.txt
  • Last modified: 2015/09/24 11:12
  • by scipio