【发布时间】:2022-01-05 04:29:48
【问题描述】:
我正在使用beautifulsoup 从xml 文档中删除一个元素。它正在删除所需的标签,但也从xml 文档中删除与该元素无关的一些其他信息。如何阻止这种情况?
要重现的代码:
import requests
from bs4 import BeautifulSoup
text_file = open('C:\Ashok\sample.xml', 'r')
s = text_file.read()
soup = BeautifulSoup(s, 'xml')
u = soup.find('Version', text='29.2.3')
fed = u.findParent()
fed.decompose()
f = open('C:\Ashok\sample.xml', "w")
f.write(str(soup))
f.close()
找到附加的比较。删除了红色矩形中显示的其他信息。
它正在更新我没有要求代码执行的页眉和页脚标签。
【问题讨论】:
标签: python xml beautifulsoup