Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีดึง Title จากหน้าเว็บด้วย BeautifulSoup


BeautifulSoup เป็น Library Python สำหรับ Parse ข้อมูลเว็บ ช่วย NLP (Natural Language Processing) วิเคราะห์ข้อมูลข้อความ สกัด Insights

NLP เป็นส่วนของ ML ที่จัดการข้อความ Pre-processing ให้เข้าโมเดล Web Scraping ใช้ได้กับ Research, Market Trends, SEO Monitoring ฯลฯ

ติดตั้ง BeautifulSoup บน Windows:

pip install beautifulsoup4

ตัวอย่าง

from bs4 import BeautifulSoup
import requests

url = "https://en.wikipedia.org/wiki/Algorithm"
req = requests.get(url)
soup = BeautifulSoup(req.text, "html.parser")
print("The titles are :")
print(soup.title)

ผลลัพธ์

The titles are : Algorithm − Wikipedia

คำอธิบาย

  • Import Packages

  • กำหนด URL

  • Request และ Parse HTML

  • ดึง Title ด้วย Attribute title

  • Print Title