【问题标题】:Prevent BeautifulSoup4 from wrapping XML with <html> and <body> tags防止 BeautifulSoup4 用 <html> 和 <body> 标签包装 XML
【发布时间】:2017-09-09 23:03:03
【问题描述】:

我使用 bs4 更改了 XML 中某些元素的一些数字字符串,但是当我将 XML 保存为文件时,我不希望 html 或 body 标记出现在 XML 中。

<annotation>
... more stuff here
</annotation>

变成

<html>
<body>
<annotation>
... more stuff here
</annotation>
</body>
</html>

加载后通过

soup = BeautifulSoup(file_obj.read(), 'lxml')

我想在保存之前美化我的 XML,但现在转换汤 -> 字符串然后扔掉我不想要的元素更容易。

链接到我正在使用的一个完整 XML 文件:https://gist.github.com/jtara1/4e583160441976e198aba2c7651aaf70

【问题讨论】:

    标签: python xml beautifulsoup bs4


    【解决方案1】:

    使用bs4,您可以将解析器引擎更改为html.parser

     soup = BeautifulSoup(file_obj.read(), 'html.parser')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 2010-12-30
      相关资源
      最近更新 更多