เราสามารถดึงเนื้อหาในหน้าเว็บจากโดเมนต่างๆ เช่น การทำเหมืองข้อมูล การดึงข้อมูล เป็นต้น ในการดึงข้อมูลจากเว็บไซต์ของหนังสือพิมพ์และนิตยสาร เราจะใช้ห้องสมุดหนังสือพิมพ์
จุดประสงค์หลักของห้องสมุดนี้คือการแยกและรวบรวมบทความจากหนังสือพิมพ์และเว็บไซต์ที่คล้ายคลึงกัน
การติดตั้ง:
-
ในการติดตั้งห้องสมุดหนังสือพิมพ์ ให้เรียกใช้ในเทอร์มินัลของคุณ:
$ 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.