BeautifulSoup,如何正确使用decompose()

时间:2018-07-15 13:45:22

标签: python xml parsing beautifulsoup

我正在尝试使用BeautifulSoup库从本地.xml文件中删除注释标签。

示例XML

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <!-- remove this -->
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
</note>

python

from bs4 import BeautifulSoup as BS
from bs4 import Comment

soup = BS(open("sample.xml"))

comments=soup.find_all(string=lambda text:isinstance(text,Comment))

for c in comments:
    c.decompose()

运行此代码时,出现以下错误:

AttributeError: 'Comment' object has no attribute 'decompose'

我不确定为什么会出现此错误。

0 个答案:

没有答案