代码示例:
from bs4 import BeautifulSoup, Comment
html_content = """
<div>
你好啊
<!--
这是注释
-->
</div>"""
soup = BeautifulSoup(html_content, 'html.parser')
for element in soup(text=lambda text: isinstance(text, Comment)):
element.extract()
print(str(soup))
运行结果:
<div>
你好啊
</div>