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")

最新文章

  1. Android ViewPager 用法
  2. Building Apps for Windows Phone 8.1教程下载地址整理
  3. Apache配置代理服务器的方法(1)
  4. javascript 网页运行代码效果
  5. 制作东皇3.2的安装U盘-黑苹果之路
  6. Spring 和 MyBatis 环境整合
  7. linux--关于shell的介绍
  8. angularJS懒加载依赖模块
  9. 开源社群系统ThinkSNS+PC端最新播报!
  10. git操作之上传gitthub
  11. C3P0 APPARENT DEADLOCK
  12. keil5一点project就闪退
  13. AI Haar特征
  14. PHP 生成水印图片
  15. python基础-----函数/装饰器
  16. POJ 3660 Cow Contest. (传递闭包)【Floyd】
  17. java Run to Line
  18. IDEA调试总结(设置断点进行调试)
  19. Spring boot starter pom的依赖关系说明
  20. git pull 总要求输入账号和密码解决?

热门文章

  1. 使用dom4j写xml文件——源码
  2. 如何迎接新的 .NET 时代
  3. SQLServer数据库系统概念
  4. git杂记-记录每次更新到仓库
  5. Maven-pom-configuration
  6. UIResponder NSSet UITouch UIEvent
  7. Python爬虫教程-08-post介绍(百度翻译)(下)
  8. HttpStatus
  9. linux day4
  10. Android根据URL下载文件保存到SD卡