用於文章抓取和策展的 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.
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP