Skip to content

Instantly share code, notes, and snippets.



Created Apr 7, 2011
What would you like to do?
lxml.etree & XPath
# -*- coding: utf-8 -*-
from lxml import etree
from StringIO import StringIO
str_html = """<p>
root = etree.parse(StringIO(str_html), etree.HTMLParser())
em1 = root.xpath('//em[1]')
print em1[0].text #aa
print etree.tostring(em1[0], method='text') #aabb
print etree.tostring(em1[0]) #<em>aa</em>bb
print etree.tostring(em1[0], method='text', with_tail=False) #aa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment