Skip to content

Instantly share code, notes, and snippets.

@ino46

ino46/gist:906840

Created Apr 7, 2011
Embed
What would you like to do?
lxml.etree & XPath
#!/usr/bin/python
# -*- coding: utf-8 -*-
from lxml import etree
from StringIO import StringIO
str_html = """<p>
<em>aa</em>bb<em>cc</em>
</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