How to get raw XML back from lxml?

I use the following code to find a div:

parser = etree.HTMLParser() tree = etree.parse(StringIO(page), parser) div = tree.xpath("//div[@class='content']")[0] 

My only problem is that after that I don't want to rely on lxml to extract the contents of the specified div: I just want to return the raw XML containing the div. Is this doable or do I need to completely abandon this method?

+4
source share
2 answers

I think you are looking for:

 etree.tostring(div) 
+6
source

Have you tried tostring ?

 raw_xml = etree.tostring(div) 
+2
source

All Articles