经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
使用Newspaper3k框架快速抓取文章信息
来源:cnblogs  作者:小张学Python  时间:2019/10/15 9:57:44  对本文有异议

一、框架介绍

    Newspaper是一个python3库,但是Newspaper框架并不适用于实际工程类新闻信息爬取工作,框架不稳定,爬取过程中会有各种bug,例如获取不到url、新闻信息等,但对于想获取一些新闻语料的朋友不妨一试,简单方便易上手,且不需要掌握太多关于爬虫方面的专业知识。

 这是 Newspaper 的github链接:

https://github.com/codelucas/newspaper

这是 Newspaper文档说明的链接:

https://newspaper.readthedocs.io/en/latest/

这是 Newspaper快速入门的链接:

https://newspaper.readthedocs.io/en/latest/user_guide/quickstart.html

安装方法:
  1. pip3 install newspaper3k

二、功能

主要功能如下:

  • 多线程文章下载框架
  • 新闻网址识别
  • 从html中提取文本
  • 从html中提取顶部图像
  • 从html中提取所有图像
  • 从文本中提取关键字
  • 从文本中提取摘要
  • 从文本中提取作者
  • Google趋势术语提取。
  • 使用10种以上语言(英语,中文,德语,阿拉伯语……)

介绍:

1.建立新闻来源
  1. import newspaper
  2. web_paper = newspaper.build("http://www.sxdi.gov.cn/gzdt/jlsc/", language="zh", memoize_articles=False)

 

  1. 注:文章缓存:默认情况下,newspaper缓存所有以前提取的文章,并删除它已经提取的任何文章。此功能用于防止重复的文章和提高提取速度。可以使用memoize_articles参数选择退出此功能。
2.提取文章的url
  1. for article in web_paper.articles:
  2. print(article.url)
  3. output:
  4. http://www.sxdi.gov.cn/gzdt/jlsc/2019101220009.html
  5. http://www.sxdi.gov.cn/gzdt/jlsc/2019101119998.html
  6. http://www.sxdi.gov.cn/gzdt/jlsc/2019100919989.html
  7. http://www.sxdi.gov.cn/gzdt/jlsc/2019100819980.html
  8. http://www.sxdi.gov.cn/gzdt/jlsc/2019092919940.html
  9. http://www.sxdi.gov.cn/gzdt/jlsc/2019092919933.html
  10. .... 
3.提取源类别
  1. for category in web_paper.category_urls():
  2. print(category)
  3. output:
  4. http://www.sxdi.gov.cn/gzdt/jlsc/....
4.提取源提要
  1. for feed_url in web_paper.feed_urls():
  2. print(feed_url)

 

  1.  

5.提取源品牌和描述

  1. print(web_paper.brand) # 品牌
  2. print(web_paper.description) # 描述
  3. print("一共获取%s篇文章" % web_paper.size()) # 文章的数目
6.下载文章
  1. from newspaper import Article
  2. article = Article("http://www.sol.com.cn/", language='zh') # Chinese
  3. article.download()
7.解析文章并提取想要的信息
  1. article.parse() #网页解析
  2. print("title=",article.title) # 获取文章标题
  3. print("author=", article.authors) # 获取文章作者
  4. print("publish_date=", article.publish_date) # 获取文章日期
  5. print("top_iamge=",article.top_image) # 获取文章顶部图片地址
  6. print("movies=",article.movies) # 获取文章视频链接
  7. print("text=",article.text,"\n") # 获取文章正文
  8. article.nlp()
  9. print('keywords=',article.keywords)#从文本中提取关键字
  10. print("summary=",article.summary)# 获取文章摘要
  11. print("images=",article.images)#html中提取所有图像
  12. print("imgs=",article.imgs)
  13. print("html=",article.html)#获取html

 

简单例子:
  1. import newspaper
  2. from newspaper import Article
  3. def spider_newspaper_url(url):
  4. """
  5. 默认情况下,newspaper缓存所有以前提取的文章,并删除它已经提取的任何文章。
  6. 使用memoize_articles参数选择退出此功能。
  7. """
  8. web_paper = newspaper.build(url, language="zh", memoize_articles=False)
  9. print("提取新闻页面的url!!!")
  10. for article in web_paper.articles:
  11. # 获取新闻网页的url
  12. print("新闻页面url:", article.url)
  13. # 调用spider_newspaper_information函数获取新闻网页数据
  14. spider_newspaper_information(article.url)
  15. print("一共获取%s篇文章" % web_paper.size()) # 文章的数目
  16.  
  17. # 获取文章的信息
  18. def spider_newspaper_information(url):
  19. # 建立链接和下载文章
  20. article = Article(url, language='zh') # Chinese
  21. article.download()
  22. article.parse()
  23. # 获取文章的信息
  24. print("title=", article.title) # 获取文章标题
  25. print("author=", article.authors) # 获取文章作者
  26. print("publish_date=", article.publish_date) # 获取文章日期
  27. # print("top_iamge=", article.top_image) # 获取文章顶部图片地址
  28. # print("movies=", article.movies) # 获取文章视频链接
  29. print("text=", article.text, "\n") # 获取文章正文
  30. print("summary=", article.summary) # 获取文章摘要
  31.  
  32.  
  33. if __name__ == "__main__":
  34. web_lists = ["http://www.sxdi.gov.cn/gzdt/jlsc/","http://www.people.com.cn/GB/59476/"]
  35. for web_list in web_lists:
  36. spider_newspaper_url(web_list)

 

原文链接:http://www.cnblogs.com/xiaozhangpython/p/11675662.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号