How do I get the full XML or HTML content of an element using ElementTree?

后端 未结 7 1137
长情又很酷
长情又很酷 2020-12-30 08:18

That is, all text and subtags, without the tag of an element itself?

Having

blah bleh blih

I

7条回答
  •  借酒劲吻你
    2020-12-30 08:58

    This answer is slightly modified of Pupeno's reply. Here I added encoding type into "tostring". This issue took many hours of mine. I hope this small correction will help others.

    def element_to_string(element):
            s = element.text or ""
            for sub_element in element:
                s += ElementTree.tostring(sub_element, encoding='unicode')
            s += element.tail
            return s
    

提交回复
热议问题