使用 Beautiful Soup 从字符串中去除 html 标签
有谁有一些示例代码来说明如何使用Python的Beautiful Soup从文本字符串中删除所有html标签(除了一些标签)?
我想删除所有 javascript 和 html 标签,除了:
<a></a>
<b></b>
<i></i>
还有类似的东西:
<a onclick=""></a>
感谢您的帮助 - 我在互联网上找不到太多用于此目的的内容。
Does anyone have some sample code that illustrates how to use Python's Beautiful Soup to strip all html tags, except some, from a string of text?
I want to strip all javascript and html tags everything except:
<a></a>
<b></b>
<i></i>
And also things like:
<a onclick=""></a>
Thanks for helping -- I couldn't find much on the internet for this purpose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
您只需要文本内容,可以将
print(tag)
更改为print(tag.string)
。如果您想从
a
标记中删除像onclick=""
这样的属性,您可以这样做:yields
If you just want the text contents, you could change
print(tag)
toprint(tag.string)
.If you want to remove an attribute like
onclick=""
from thea
tag, you could do this: