2010-07-13 47 views

回答

3

我必须自动为我的一个项目做到这一点。我找到的解决方案是使用Beautiful Soup模块来提取脚本标记(我也是这样做的风格和形式)。

soup = BeautifulSoup(html_string, convertEntities=BeautifulSoup.HTML_ENTITIES) 

scripts = soup.findAll('script') # find and return a list of 'script' entities 
for s in scripts: 
    s.extract() # remove it from the DOM completely 

然后,你可以有BeautifulSoup打印出来或保存html。

0

另一种选择,专为消毒,是html5lib

无论你做什么,做而不是依靠一个编辑器组件来为你做:它运行在客户端,所以很容易被操纵提交无效或恶意的HTML!