版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sty945/article/details/87901407
前言
随着越来的进行自然语言处理相关方面的研究,但是,往往一些文本数据,我们很难去获得,那么如何快速的获取网页数据呢?最常用的方法就是自己写爬虫或者借助别人的爬虫框架来爬取网站,当时这个往往学习成本比较高**,那么我们有没有什么捷径可以很快的的通过别人已经定义好的爬虫库来爬取新闻网页数据呢?**
一个爬取新闻网页数据的神器
介绍python一个文章爬取和管理的神器,当前在Github上fork的人数已经过了7K了,并且还是持续增加
小试牛刀
我现在想爬取一下这个网页的数据:
http://www.aihami.com/a/dangjian/shibada/400787.html
我只需如下几行代码就行了
from newspaper import Article
url = 'http://www.aihami.com/a/dangjian/shibada/400787.html'
article = Article(url, language='zh') # Chinese
article.download()
article.parse()
print(article.text[:])
然后可以发现我已经得到了网页上的新闻内容
我这里是输出了全部的内容,当然你也可以选择性输出,比如:
- 只输出内容:
print(article.text)
- 只输出标题
print(article.title)
- 输出最上面的图片:
print(article.top_image)
如何快速安装
windows安装
官方建议使用python3,如果没有意外的话,你完全可以通过Anaconda环境下通过以下命令安装(本人win10系统)
pip3 install newspaper3k
Debian / Ubuntu安装
不太确定是否装了Anaconda之后,可以直接:
pip3 install newspaper3k
大家可以试一试
扫描二维码关注公众号,回复:
5311664 查看本文章
如果不行,官方建议按照如下步骤
sudo apt-get install python3-pip
sudo apt-get install python3-pip
sudo apt-get install libxml2-dev libxslt-dev
sudo apt-get install libjpeg-dev zlib1g-dev libpng12-dev
如果安装libpng12-dev
有问题,尝试安装libpng-dev
然后进行
curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python3
pip3 install newspaper3k
OSX安装
可以直接:
pip3 install newspaper3k
如果不行,官方建议
$ brew install libxml2 libxslt
$ brew install libtiff libjpeg webp little-cms2
$ pip3 install newspaper3k
$ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python3
体验更多的功能
这个库还有其他更多的功能,大家可以参考它的官方文档:
https://newspaper.readthedocs.io/en/latest/user_guide/quickstart.html#
也可以查看它的官方源代码:
https://github.com/codelucas/newspaper