Computer >> Máy Tính >  >> Lập trình >> Python

Mô-đun Python Báo cho bài viết cóp nhặt &giám tuyển?

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

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

Cài đặt:

  • Để cài đặt thư viện Báo, hãy chạy trong thiết bị đầu cuối của bạn:

$ pip install newspaper3k
  • Đối với các phụ thuộc lxml, hãy chạy lệnh bên dưới trong thiết bị đầu cuối của bạn

$pip install lxml
  • Để cài đặt PIL, hãy chạy

$pip install Pillow
  • Kho tài liệu NLP sẽ được tải xuống:

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

Thư viện newpaper của python được sử dụng để thu thập thông tin liên quan đến các bài báo. Điều này bao gồm tên tác giả, các hình ảnh chính trong bài báo, ngày xuất bản, video có trong bài báo, các từ chính mô tả bài báo và phần tóm tắt của bài báo.

#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)

Đầu ra:

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

Đầu ra:

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)

Dưới đây là toàn bộ chương trì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)

Đầu ra:

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