python爬虫智能解析之newspaper
前言:
最近抓取了某查新闻舆情维度的数据,业务需求做一个情感分析,需要抓取相关链接的正文(因某查抓下来的只有摘要),新闻舆情的链接已经抓下来了,但是如下图所示这么多网站:
我总不能每个网站写一个爬虫抓正文吧,那不得累死!于是想起来之前听过的智能化解析,遂学习一番,现需求任务已完成,在此做个记录总结与分享.
1. newspaper简介、特点及安装
-
简介
newspaper库分为Python2和Python3两个版本,Python2下的版本叫做newspaper,Python3下的版本叫做newspaper3k,Python2版本的不做说明,下面使用Python3版本来进行测试。
-
特点
- 多线程文章下载框架
- 新闻网址识别
- 从 html 中提取文本
- 从html中提取顶部图像
- 从html中提取所有图像
- 从文本中提取关键字
- 从文本中提取摘要
- 从文本中提取作者
- 谷歌趋势词提取
- 支持 10 多种语言(英语、中文、德语、阿拉伯语……)
-
安装
-
其GitHub地址是:https://github.com/codelucas/newspaper
-
官方文档地址是:https://newspaper.readthedocs.io
-
在安装之前需要安装一些依赖库:
beautifulsoup4>=4.4.1 cssselect>=0.9.2 feedfinder2>=0.0.4 feedparser>=5.2.1 jieba3k>=0.35.1 lxml>=3.6.0 nltk>=3.2.1 Pillow>=3.3.0 pythainlp>=1.7.2 python-dateutil>=2.5.3 PyYAML>=3.11 requests>=2.10.0 tinysegmenter==0.3 # TODO(codelucas): Investigate making this >=0.3 tldextract>=2.0.1也可以参考官方的说明:https://github.com/codelucas/newspaper#get-it-now。
-
这些依赖库安装完成之后,使用pip安装newspaper3k
pip3 install newspaper3k至此已安装完成,之后就可以愉快的使用了
-
2. newspaper的使用
-
官方文档给出的是英文网站,上代码感受一下
>>> from newspaper import Article>>> url = 'http://fox13now.com/2013/12/30/new-year-new-laws-obamacare-pot-guns-and-drones/' >>> article = Article(url)>>> article.download()>>> article.html '>>> article.parse()>>> article.authors ['Leigh Ann Caldwell', 'John Honway']>>> article.publish_date datetime.datetime(2013, 12, 30, 0, 0)>>> article.text 'Washington (CNN) -- Not everyone subscribes to a New Year's resolution...'>>> article.top_image 'http://someCDN.com/blah/blah/blah/file.png'>>> article.movies ['http://youtube.com/path/to/link.com', ...]>>> article.nlp()>>> article.keywords ['New Years', 'resolution', ...]>>> article.summary 'The study shows that 93% of people ...'可以看到,首先要导入Article类,直接传入url参数,其次调用download()方法,将网页加载出来,然后调用parse()方法对网页内容进行智能解析,可以解析出来authors,publish_date,title,text,image,movies等等.
另外还有个nlp()方法,可以提取keywords,summary等等.详细的可以查看官方文档.
-
我需要抓取的是中文网站,Article中还需传入language,支持的语言如下:
>>> import newspaper >>> newspaper.languages()Your available languages are:input code full namear Arabicbe Belarusianbg Bulgarianda Danishde Germanel Greeken Englishes Spanishet Estonianfa Persianfi Finnishfr Frenchhe Hebrewhi Hindihr Croatianhu Hungarianid Indonesianit Italianja Japaneseko Koreanmk Macedoniannb Norwegian (Bokmål)nl Dutchno Norwegianpl Polishpt Portuguesero Romanianru Russiansl Sloveniansr Serbiansv Swedishsw Swahilitr Turkishuk Ukrainianvi Vietnamesezh Chinese -
直接上代码:
from newspaper import Articleurl = '' try:article = Article(url, language='zh')article.download()print('html:', article.html)article.parse()print('authors:', article.authors)print('title:', article.title)print('date:', article.publish_date)print('text:', article.text)print('text_len:', len(article.text))print('top image:', article.top_image)print('images:', article.images)print('movies:', article.movies)# article.nlp()# print('keywords:', article.keywords)# print('summary:', article.summary)except newspaper.article.ArticleException:print(e)如果download()出错,会抛出一个newspaper.article.ArticleException的异常.
-
到这里就结束啦,虽然很简单,但是newspaper确实给我省了不少事情,不过有一些特殊的网站提取不到,准确率还算可以吧.
-
还有一个Diffbot,好像更好用,只是需要工作邮箱,各种原因注册不上,试用期两周之后会收费.
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
