Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Module Newspaper của Python cho cài đặt và tuyển tập bài báo

Chúng ta có thể trích xuất nội dung từ trang web của nhiều miền như khai thác dữ liệu, truy xuất thông tin, v.v. Để trích xuất thông tin từ trang web báo chí và tạp chí, chúng ta sẽ sử dụng thư viện newspaper.

Mục đích chính của thư viện này là trích xuất và tuyển tập bài viết từ báo và các trang web tương tự.

Cài đặt

  • Để cài đặt thư viện Newspaper, chạy trong terminal:
$ pip install newspaper3k
  • Cho các phụ thuộc lxml, chạy lệnh sau:
$pip install lxml
  • Để cài đặt PIL, chạy:
$pip install Pillow
  • Corpus NLP sẽ được tải xuống:
$ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python

Thư viện newspaper của Python dùng để thu thập thông tin liên quan đến bài viết. Bao gồm tên tác giả, hình ảnh chính trong bài, ngày xuất bản, video có trong bài, từ khóa mô tả bài và tóm tắt bài viết.

# Nhập thư viện cần thiết
from newspaper import Article

# URL liên kết - muốn trích xuất
url = "https://www.wsj.com/articles/lawmakers-to-resume-stalled-border-security-talks-11549901117"

# Tải bài viết
>>> from newspaper import Article
>>> url = "https://www.wsj.com/articles/lawmakers-to-resume-stalled-border-security-talks-11549901117"
>>> article = Article(url)
>>> article.download()

# Phân tích bài viết và lấy tên tác giả
>>> article.parse()
>>> print(article.authors)

Kết quả:

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

# Trích xuất ngày xuất bản
>>> print("Article Publication Date:")
>>> print(article.publish_date)

# Trích xuất URL hình ảnh chính
>>> print(article.top_image)

Kết quả:

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

# Trích xuất từ khóa bằng NLP
print ("Keywords in the article", article.keywords)

# Trích xuất tóm tắt bài viết
print("Article Summary", article.summary)

Dưới đây là chương trình hoàn chỉnh:

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)

Kết quả:

['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.