newspaper库是一个主要用来提取新闻内容及分析的Python爬虫框架。此库适合抓取新闻网页。操作简单易学,即使对完全没了解过爬虫的初学者也非常的友好,简单学习就能轻易上手,除此之外,使用过程你不需要考虑HTTP Header、IP代理,也不需要考虑网页解析,网页源代码架构等问题。

我们以https://www.wired.com/为例,进行演示。

  1. 获取新闻

import newspaper
from newspaper import Article
from newspaper import fulltext
url = 'https://www.wired.com/'
paper = newspaper.build(url, language="en", memoize_articles=False)

输出新闻对象

<newspaper.source.Source object at 0x7fe82c98c1d0>

默认情况下,newspaper 缓存所有以前提取的文章,并删除它已经提取的任何文章,使用 memoize_articles 参数选择退出此功能。

  1. 提取新闻URL

提取站点页面的新闻URL

import newspaper
from newspaper import Article
from newspaper import fulltext
url = 'https://www.wired.com/'
paper = newspaper.build(url, language="en", memoize_articles=False)
for article in paper.articles:
print(article.url)

输出内容

  1. 提取新闻分类

支持提取站点下的新闻分类

for category in paper.category_urls():
print(category)

  1. 提取新闻内容:Article

文章对象是新闻文章的抽象。例如,新闻Source将是Wired,而新闻Article是其站点下的Wired文章,这样就可以提取出新闻的标题、作者、插图、内容等。

article = Article('https://www.wired.com/story/preterm-babies-lonely-terror-of-a-pandemic-nicu/')
article.download()
article.parse()
print("title=", article.title)
print("author=", article.authors)
print("publish_date=", article.publish_date)
print("top_iamge=", article.top_image)
print("movies=", article.movies)
print("text=", article.text)
print("summary=", article.summary)

  1. 下载解析

我们选取其中一篇文章为例,如下所示:

first_url = paper.articles[0]
first_url.download()
first_url.parse()
print(first_url.title)
print(first_url.publish_date)
print(first_url.authors)
print(first_url.top_image)
print(first_url.summary)
print(first_url.movies)
print(first_url.text)

  1. 解析html

通过 requests 库获取文章 html 信息,用 newspaper 进行解析,如下所示:

html = requests.get('https://www.wired.com/story/preterm-babies-lonely-terror-of-a-pandemic-nicu/').text
print('获取的原信息-->', html)
text = fulltext(html, language='en')
print('解析后的信息', text)
  1. 结合nlp

通过使用nlp方法,可以从文本中提取自然语言属性。

first_article = paper.articles[1]
first_article.download()
first_article.parse()
first_article.nlp()
print(first_article.summary)
print(first_article.keywords)
  1. 多任务

当我们需要从多个渠道获取新闻信息时可以采用多任务的方式,如下所示:

import newspaper
from newspaper import news_pool
lr_paper = newspaper.build('https://lifehacker.com/', language="en")
wd_paper = newspaper.build('https://www.wired.com/', language="en")
ct_paper = newspaper.build('https://www.cnet.com/news/', language="en")
papers = [lr_paper, wd_paper, ct_paper]
# 线程数为 3 * 2 = 6
news_pool.set(papers, threads_per_source=2)
news_pool.join()
print(lr_paper.articles[0].html)
  1. 其他

hot()返回Google上最热门的术语列表。

popular_urls()返回热门新闻来源网址的列表。

newspaper.hot()
newspaper.popular_urls()

新闻类爬虫库:Newspaper的更多相关文章

  1. 基于php编写的新闻类爬虫,插入WordPress数据库

    这个爬虫写的比较久远,很久没有更新博客了. 1.首先思路是:通过php的curl_setopt()函数可以方便快捷的抓取网页. 2.什么样的新闻吸引人呢,当然的热点新闻了.这里选百度的搜索风云榜,获取 ...

  2. GNE: 4行代码实现新闻类网站通用爬虫

    GNE(GeneralNewsExtractor)是一个通用新闻网站正文抽取模块,输入一篇新闻网页的 HTML, 输出正文内容.标题.作者.发布时间.正文中的图片地址和正文所在的标签源代码.GNE在提 ...

  3. 强大的金融类图表库 TradingView 使用分享

    这段时间刚好做币圈交易所,运用到了现在最火的金融类图表库 -- TradingView ,就是强大,基本上现在的火币网(https://www.huobi.com),币安网(https://www.b ...

  4. 13.CrawlSpider类爬虫

    1.CrawlSpider介绍 Scrapy框架中分两类爬虫,Spider类和CrawlSpider类. 此案例采用的是CrawlSpider类实现爬虫. 它是Spider的派生类,Spider类的设 ...

  5. music-api-next:一款支持网易、xiami和QQ音乐的JS爬虫库

    音乐,无界 让音乐无界 如果你苦于挑选一个全方位.多平台.简便易用的音乐爬虫库,music-api-next是不二选择. 特性: 支持网易.虾米和QQ三大主流音乐平台 支持音乐关键词搜索 支持音乐链接 ...

  6. Scrapy框架——CrawlSpider类爬虫案例

    Scrapy--CrawlSpider Scrapy框架中分两类爬虫,Spider类和CrawlSpider类. 此案例采用的是CrawlSpider类实现爬虫. 它是Spider的派生类,Spide ...

  7. 爬虫笔记之刷小怪练级:yymp3爬虫(音乐类爬虫)

    一.目标 爬取http://www.yymp3.com网站歌曲相关信息,包括歌曲名字.作者相关信息.歌曲的音频数据.歌曲的歌词数据. 二.分析 2.1 歌曲信息.歌曲音频数据下载地址的获取 随便打开一 ...

  8. Python3 常用爬虫库的安装

    Python3 常用爬虫库的安装 1 简介 Windows下安装Python3常用的爬虫库:requests.selenium.beautifulsoup4.pyquery.pymysql.pymon ...

  9. 使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解(新手必学)

    为大家介绍下Python爬虫库BeautifulSoup遍历文档树并对标签进行操作的详细方法与函数下面就是使用Python爬虫库BeautifulSoup对文档树进行遍历并对标签进行操作的实例,都是最 ...

随机推荐

  1. centos7安装宝塔面板

    在终端下执行如下命令 yum install -y wget && wget -O install.sh http://download.bt.cn/install/install.s ...

  2. C# url的编码解码,xml和json的序列化和反序列化

    参考中国慕课网dot net web编程应用程序实践 using System; using System.Collections.Generic; using System.IO; using Sy ...

  3. 【pytest】(十二)参数化测试用例中的setup和teardown要怎么写?

    还是一篇关于pytest的fixture在实际使用场景的分享. fixture我用来最多的就是写setup跟teardown了,那么现在有一个用例是测试一个列表接口,参数化了不同的状态值传参,来进行测 ...

  4. 使用eventfd创建一个用于事件通知的文件描述符

    https://www.jianshu.com/p/57cc1d7d354f nat穿透代码c++

  5. Spring常见问题总结

    1. 什么是 Spring 框架? Spring 是一种轻量级开发框架,旨在提高开发人员的开发效率以及系统的可维护性.Spring 官网:https://spring.io/. 我们一般说 Sprin ...

  6. BZOJ1031

    前一段时间终于看明白了后缀数组,记录一下主要的做过的题目,主要的按照黄学长的BLOG作的,主要是为了记模板.原理还是上网自己查一下吧!代码会加简单的注释. ********************** ...

  7. xss靶场详解

    一个XSS靶场练习记录 https://blog.csdn.net/qq_41500251/article/details/101116697 001.level 1 反射型 1.观察源码 <s ...

  8. Java——时间和日期处理

    Date Date date = new Date(); 获取时间 Date d = new Date(); // Date d2=new Date(System.currentTimeMillis( ...

  9. 渗透测试工具-sqlmap

    简单来说:一个用来做sql注入攻击的工具 安装 1,下载sqlmap.zip,下载环境: 打开sqlmap官网https://github.com/sqlmapproject/sqlmap/ :下载p ...

  10. 跨边界传输之反弹shell

    反弹shell     1.nc         正向连接             攻击机                 nc-vv 受害者ip 受害者port             受害者    ...