Python BeautifulSoup:去除 HTML 中的 script


#Python BeautifulSoup 教程


代码示例:

from bs4 import BeautifulSoup

html_content = '''
<script>a</script>

    你好啊<p>hi</p>

<script>b</script>
'''
soup = BeautifulSoup(html_content, 'html.parser')
for s in soup('script'):
    s.extract()

new_html_content = str(soup)
print(new_html_content)

运行结果:




    你好啊<p>hi</p>





( 本文完 )