Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get raw XML back from lxml?

I'm using the following code to locate a div:

parser = etree.HTMLParser()
tree = etree.parse(StringIO(page), parser)

div = tree.xpath("//div[@class='content']")[0]

My only problem is, that after doing this I do not want to rely on lxml to extract the contents of said div: I just want to get back the raw XML the div contains. Is this doable or do I have to abandon this method entirely?

like image 373
akosch Avatar asked Oct 24 '10 00:10

akosch


2 Answers

I think you are looking for:

etree.tostring(div)
like image 91
unutbu Avatar answered Oct 06 '22 14:10

unutbu


Did you try tostring?

raw_xml = etree.tostring(div)
like image 42
Ryan Ginstrom Avatar answered Oct 06 '22 13:10

Ryan Ginstrom