python过滤文件中特殊标签
2024-10-20 05:52:12
Beautiful Soup
Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。它可以大大节省你的编程时间。 对于Ruby,使用Rubyful Soup。
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
# 添加文章,并且过滤文章内容
def add_artical(request, username):
if request.method == "POST":
user = request.user
artical_title = request.POST.get("artical_title")
artical_content = request.POST.get("artical_content")
# desc = artical_content[0:150] # 解释html标签
from bs4 import BeautifulSoup
# html.parser为解析器,是python标准库
bs = BeautifulSoup(artical_content, "html.parser")
desc = bs.text[0:150] + "..." # 过滤非法标签
for tag in bs.find_all():
if tag.name in ["script", "link"]:
# 将该非法标签从对象中移除
tag.decompose() # 打印结果为"123 <class 'bs4.BeautifulSoup'>"
print(bs,type(bs)) try:
artical_obj = models.Artical.objects.create(user=user, desc=desc, title=artical_title)
models.ArticalDetail.objects.create(content=str(bs), artical=artical_obj)
except:
return HttpResponse("更新文章失败 ")
return HttpResponse("添加成功") return render(request, "add_artical.html")
最新文章
- Android ViewPager 用法
- Building Apps for Windows Phone 8.1教程下载地址整理
- Apache配置代理服务器的方法(1)
- javascript 网页运行代码效果
- 制作东皇3.2的安装U盘-黑苹果之路
- Spring 和 MyBatis 环境整合
- linux--关于shell的介绍
- angularJS懒加载依赖模块
- 开源社群系统ThinkSNS+PC端最新播报!
- git操作之上传gitthub
- C3P0 APPARENT DEADLOCK
- keil5一点project就闪退
- AI Haar特征
- PHP 生成水印图片
- python基础-----函数/装饰器
- POJ 3660 Cow Contest. (传递闭包)【Floyd】
- java Run to Line
- IDEA调试总结(设置断点进行调试)
- Spring boot starter pom的依赖关系说明
- git pull 总要求输入账号和密码解决?