How to get raw XML back from lxml?

ぃ、小莉子 提交于 2019-12-04 01:09:30

问题


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?


回答1:


I think you are looking for:

etree.tostring(div)



回答2:


Did you try tostring?

raw_xml = etree.tostring(div)


来源:https://stackoverflow.com/questions/4006668/how-to-get-raw-xml-back-from-lxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!