0
這裏是我的Python代碼LXMLLXML刪除展開文本標記內
import urllib.request
from lxml import etree
#import lxml.html as html
from copy import deepcopy
from lxml import etree
from lxml import html
some_xml_data = "<span>text1<div>ddd</div>text2<div>ddd</div>text3</span>"
root = etree.fromstring(some_xml_data)
[c] = root.xpath('//span')
print(etree.tostring(root)) #b'<span>text1<div>ddd</div>text2<div>ddd</div>text3</span>' #output as expected
#but if i do some changes
for e in c.iterchildren("*"):
if e.tag == 'div':
e.getparent().remove(e)
print(etree.tostring(root)) #b'<span>text1</span>' text2 and text3 removed! how to prevent this deletion?
它看起來像後,我做LXML樹一些變化(刪除一些標籤) LXML還刪除了一些解開的文字!如何防止lxml這樣做並保存unwrpapped文本?