有什么方法可以从HTML文档中删除JavaScript代码?

qua*_*ity 3 html javascript python

我想从HTML文档中删除所有javascript代码,并保留实际文本。是否有任何正则表达式或python脚本可以做到这一点?谢谢。

ick*_*fay 5

使用BeautifulSoup:

#!/usr/bin/env python
from BeautifulSoup import BeautifulSoup

with open("with-scripts.html", "r") as f:
    soup = BeautifulSoup(f.read())

for script in soup("script"):
    script.extract()

with open("without-scripts.html", "w") as f:
    f.write(soup.prettify())
Run Code Online (Sandbox Code Playgroud)