首页 > 其他 > 详细

BeautifulSoup实现博文简介与过滤恶意标签(xxs攻击)

时间:2018-09-10 22:55:40      阅读:357      评论:0      收藏:0      [点我收藏+]
一、BeautifulSoup模块
二、博文简介
三、过滤恶意标签
 
 
一、BeautifulSoup模块
pip install bs4  # 安装bs4
 
from bs4 import BeautifulSoup  # 导入BeautifulSoup
 
二、博文简介
from bs4 import BeautifulSoup
 
content = ‘<a href="http://example.com/">I linked to <i>example.com</i></a>‘
soup = BeautifulSoup(content, ‘html.parser‘)
overview = soup.text[0:9]
print(overview)
 
三、过滤恶意标签
from bs4 import BeautifulSoup
 
content = ‘<a href="http://example.com/">I linked to <i>example.com</i></a><div><img src=""></img>image</div><a>link</a><script>alert(123)</script>‘
soup = BeautifulSoup(content, ‘html.parser‘)
print(soup)  # 这里带有script标签的脚本
 
for tag in soup.find_all():
    if tag.name in [‘script‘, ‘link‘]:
        tag.decompose()
 
print(soup)  # 这里已经把带有script标签的脚本去掉了
 
 

BeautifulSoup实现博文简介与过滤恶意标签(xxs攻击)

原文:https://www.cnblogs.com/changwoo/p/9623487.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!