เราสามารถดึงเนื้อหาจากหน้าเว็บหลากหลายโดเมนได้ เช่น การขุดข้อมูล การเรียกข้อมูล สำหรับเว็บไซต์หนังสือพิมพ์และนิตยสาร เราจะใช้ไลบรารี newspaper3k
การติดตั้ง
$ pip install newspaper3k
$ pip install lxml
$ pip install Pillow
# ดาวน์โหลด NLP corpora
$ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python
ความสามารถหลัก
ไลบรารีนี้ดึงข้อมูลได้ดังนี้:
- ชื่อผู้เขียน (authors)
- รูปภาพหลัก (top image)
- วันที่เผยแพร่ (publish date)
- วิดีโอในบทความ
- คีย์เวิร์ด (keywords)
- สรุปเนื้อหา (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.publish_date)
print("รูปภาพหลัก:", article.top_image)
# ใช้ NLP ดึงคีย์เวิร์ดและสรุป
article.nlp()
print("คีย์เวิร์ด:", article.keywords)
print("สรุป:", article.summary)
ผลลัพธ์ตัวอย่าง
ผู้เขียน: ['Kristina Peterson', 'Andrew Duehren', 'Natalie Andrews', ...]
วันที่เผยแพร่: 2019-02-11 00:00:00
รูปภาพหลัก: https://images.wsj.net/im-51122/social
คีย์เวิร์ด: ['state', 'spending', 'sweeping', 'southern', 'security', 'border', ...]
สรุป: President Trump made the case in his State of the Union address for the construction of a wall along the southern U.S. border...
โค้ดเต็มรูปแบบ
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)
ประยุกต์ใช้
- สร้างระบบสรุปข่าวอัตโนมัติ
- วิเคราะห์แนวโน้มหัวข้อข่าว
- ดึงข้อมูลสำหรับการวิจัยหรือ Data Mining