用于文章抓取和策展的 Python 模块 Newspaper?


我们可以从各种域(如数据挖掘、信息检索等)的网页中提取内容。要从新闻网站和杂志提取信息,我们将使用 newspaper 库。

此库的主要目的是从报纸和类似网站提取和汇总文章。

安装

  • 要安装 Newspaper 库,请在终端中运行

$ pip install newspaper3k
  • 对于 lxml 依赖项,请在终端中运行以下命令

$pip install lxml
  • 要安装 PIL,请运行

$pip install Pillow
  • 将下载 NLP 语料库

$ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python

python newpaper 库用于收集与文章关联的信息。这包括作者姓名、文章中的主要图片、出版日期、文章中出现的视频、描述文章的关键词和文章摘要。

#Import required library
from newspaper import Article
# url link-which you want to extract
url = "https://www.wsj.com/articles/lawmakers-to-resume-stalled-border-security-talks-11549901117"
# Download the article
>>> from newspaper import Article
>>> url = "https://www.wsj.com/articles/lawmakers-to-resume-stalled-border-security-talks-11549901117"
>>> article = Article(url)
>>> article.download()
# Parse the article and fetch authors name
>>> article.parse()
>>> print(article.authors)

输出

['Kristina Peterson', 'Andrew Duehren', 'Natalie Andrews', 'Kristina.Peterson Wsj.Com', 'Andrew.Duehren Wsj.Com', 'Natalie.Andrews Wsj.Com']

# Extract Publication date
>>> print("Article Publication Date:")
>>> print(article.publish_date)

# Extract URL of the major images

>>> print(article.top_image)

输出

https://images.wsj.net/im-51122/social

# Extract keywords using NLP

print ("Keywords in the article", article.keywords)

# Extract summary of the article

print("Article Summary", article.summary)

以下是完整程序

from newspaper import Article
url = "https://www.wsj.com/articles/lawmakers-to-resume-stalled-border-security-talks-11549901117"
article = Article(url)
article.download()
article.parse()
print(article.authors)
print("Article Publication Date:")
print(article.publish_date)
print("Major Image in the article:")
print(article.top_image)
article.nlp()
print ("Keywords in the article")
print(article.keywords)
print("Article Summary")
print(article.summary)

输出

['Kristina Peterson', 'Andrew Duehren', 'Natalie Andrews', 'Kristina.Peterson Wsj.Com', 'Andrew.Duehren Wsj.Com', 'Natalie.Andrews Wsj.Com']
Article Publication Date:
None
Major Image in the article:
https://images.wsj.net/im-51122/social
Keywords in the article
['state', 'spending', 'sweeping', 'southern', 'security', 'border', 'principle', 'lawmakers', 'avoid', 'shutdown', 'reach', 'weekendthe', 'fund', 'trump', 'union', 'agreement', 'wall']
Article Summary
President Trump made the case in his State of the Union address for the construction of a wall along the southern U.S. border, calling it a “moral issue."
Photo: GettyWASHINGTON—Senior lawmakers said Monday night they had reached an agreement in principle on a sweeping deal to end a monthslong fight over border security and avoid a partial government shutdown this weekend.
The top four lawmakers on the House and Senate Appropriations Committees emerged after three closed-door meetings Monday and announced that they had agreed to a framework for all seven spending bills whose funding expires at 12:01 a.m. Saturday.

更新于:30-7-2019

406 次观看

开启你的 职业生涯

完成课程获得认证

开始学习
广告