如何删除< script>之间的文本和< / script>用python? [英] how to remove text between <script> and </script> using python?

查看:84
本文介绍了如何删除< script>之间的文本和< / script>用python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python删除< script> < / script> 之间的文字?

how to remove text between <script> and </script> using python?

推荐答案

您可以使用 BeautifulSoup 使用此(和其他)方法:

You can use BeautifulSoup with this (and other) methods:

soup = BeautifulSoup(source.lower())
to_extract = soup.findAll('script')
for item in to_extract:
    item.extract()

这实际上从HTML中删除了节点。如果您想保留空的< script>< / script> 标记,则必须使用属性,而不仅仅是从汤中提取它。

This actually removes the nodes from the HTML. If you wanted to leave the empty <script></script> tags you'll have to work with the item attributes rather than just extracting it from the soup.

这篇关于如何删除&lt; script&gt;之间的文本和&lt; / script&gt;用python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆