示例
from bs4 import BeautifulSoup
html_content = '''
<div id="content">
<p>测试01</p>
<span>测试02</span>
</div>
<div>测试03</div>
'''
soup = BeautifulSoup(html_content, 'html.parser')
print('------清空前-----')
print(soup)
content_div = soup.select_one("#content")
content_div.clear() # 清理
print('------清空后-----')
print(soup)
执行结果:
------清空前-----
<div id="content">
<p>测试01</p>
<span>测试02</span>
</div>
<div>测试03</div>
------清空后-----
<div id="content"></div>
<div>测试03</div>