示例
代码:
from bs4 import BeautifulSoup
html_content = '''
<div id="content" data="你好">
<p>测试01</p>
<span>测试02</span>
</div>
<div>测试03</div>
'''.strip()
soup = BeautifulSoup(html_content, 'html.parser')
print('----- 处理前 -----')
print(soup)
print('----- 删除 -----')
content_div = soup.select_one('div#content')
content_div.extract()
print('----- 处理后 -----')
print(soup)
执行结果:
----- 处理前 -----
<div data="你好" id="content">
<p>测试01</p>
<span>测试02</span>
</div>
<div>测试03</div>
----- 删除 -----
----- 处理后 -----
<div>测试03</div>