分享

傻瓜式文章爬虫-newspaper库简介

 大邓的Python 2021-02-23

今天比较闲,我就浏览了会github上有关python爬虫的项目。看到一个newspaper库,关注数挺高的。作者受lxml的强大和requests的简洁,开发了newspaper库。

requests库的作者都盛赞newspaper库的牛B。

  1. "Newspaper is an amazing python library for extracting & curating

  2. articles." -- tweeted by Kenneth Reitz, Author of requests

一、newspaper特性

  • 多进程文章下载框架

  • 新闻链接识别

  • 可从html文件中提取文本、图片

  • 可文章关键词提取

  • 可生成文章概要

  • 提取文章作者名

  • 谷歌趋势词提取

  • 支持十数种语言(含中文)

其实之前我写过一个类似的库的介绍-goose(仅支持python2),跟newspaper有类似功能。 文章名《不会写爬虫的快来goose一下》

二、安装

  1. pip3 install newspaper3k

注意:在python3中安装,必须是newspaper3k。 newspaper是python2上的库。

三、开始代码

3.1newspaper支持的语言

  1. import newspaper

  2. print(newspaper.languages())

  1. Your available languages are:

  2. input code        full name

  3.  ar              Arabic

  4.  da              Danish

  5.  de              German

  6.  el              Greek

  7.  en              English

  8.  es              Spanish

  9.  fi              Finnish

  10.  fr              French

  11.  he              Hebrew

  12.  hu              Hungarian

  13.  id              Indonesian

  14.  it              Italian

  15.  ko              Korean

  16.  mk              Macedonian

  17.  nb              Norwegian (Bokmål)

  18.  nl              Dutch

  19.  no              Norwegian

  20.  pt              Portuguese

  21.  ru              Russian

  22.  sv              Swedish

  23.  tr              Turkish

  24.  vi              Vietnamese

  25.  zh              Chinese

3.2 文章内容提取

提取文章内容,如作者、出版日期、文章内容、图片链接

  1. from newspaper import Article

  2. url = 'http://media.china.com.cn/cmyw/2017-06-13/1067887.html'

  3. article = Article(url, language='zh')

  4. #下载文章

  5. article.download()

  6. #查看文章的html数据

  7. #print(article.html)

  8. #解析文章html数据

  9. article.parse()

  10. #提取各种数据信息

  11. #作者

  12. print(article.authors)

  13. #出版日期

  14. print(article.publish_date)

  15. #新闻内容

  16. print(article.text)

  17. #文章的首图链接

  18. print(article.top_image)

3.3 自然语言处理

继续3.2部分代码

  1. #nlp初始化

  2. article.nlp()

  3. #提取关键词

  4. print(article.keywords)

  5. #文章概要

  6. print(article.summary)

3.4 更精细的使用方法

上面的方法是默认的方法,如果你确定某网站采用的全部是一种语言,你可以使用下面代码

  1. #文档中使用的案例

  2. import newspaper

  3. sina_paper = newspaper.build('http://www.sina.com.cn/', language='zh')

  4. for category in sina_paper.category_urls():

  5.    print(category)

输出了新浪网所有栏目

  1. http://roll.fashion.sina.com.cn

  2. http://www.sina.com.cn/

  3. http://hainan.sina.com.cn

  4. http://jiangsu.sina.com.cn

  5. http://vr.sina.cn

  6. http://cq.auto.sina.com.cn

  7. http://eladies.sina.com.cn

  8. http://chuangye.sina.com.cn

  9. http://gx.sina.com.cn

  10. http://slide.mil.news.sina.com.cn

  11. http://hlj.sina.com.cn

  12. http://history.sina.com.cn

  13. .

  14. .

  15. .

  16. http://tech.sina.com.cn//nmg.sina.com.cn

  17. http://shiqu.sina.com.cn

  18. http://ah.sina.com.cn

  19. http://slide.news.sina.com.cn

  20. http://chexian.sina.com

总结,用着效果没有requests作者夸的那么棒,可能我找的网站正好是newspaper无法完美处理的网站。

Tips:这篇文章抓的是外国网站-Twitter每日推荐导读

《借助Python Newspaper库创建Read It Later App》

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多