สอน Web Scraping ด้วย Gazpacho ใน Python

Gazpacho is a web scraping package in Python.

🤨 Web Scraping คืออะไร?

Web scraping คือ การดึงข้อมูลจากหน้าเว็บโดยอัตโนมัติผ่านเครื่องมือต่าง ๆ ซึ่งใช้ประโยชน์ได้หลายอย่าง เช่น:

  • เปรียบเทียบราคาสินค้า
  • ติดตามกระแสตอบรับ brand
  • รวบรวมข้อมูลสำหรับพัฒนา LLM

💻 Web Scraping ใน Python: BeautifulSoup vs Gazpacho

ใน Python, package ที่นิยมใช้สำหรับ web scraping คือ BeautifulSoup

แต่ BeautifulSoup มีข้อจำกัด เนื่องจากไม่สามารถโหลดข้อมูลจากหน้าเว็บเองได้ ทำให้ต้องใช้ package อื่นอย่าง requests เข้ามาช่วย

Gazpacho เป็น package ที่ออกแบบมาเพื่อทดแทน BeautifulSoup โดยเน้นความเร็วในการทำงาน และการทำงานครบ loop ได้ด้วยตัวเอง


🍜 วิธีใช้ Gazpacho

เราใช้ Gazpacho ดึงข้อมูลจากเว็บไซต์ได้ง่าย ๆ ใน 3 ขั้นตอน

  1. Get web page
  2. Create Soup object
  3. Extract content

เราไปดูวิธีใช้ Gazpacho ผ่านตัวอย่างการดึงข้อมูลหนังสือ Sapiens: A Brief History of Humankind จาก Book to Scrape ซึ่งเป็นเว็บไซต์สำหรับฝึก web scraping กัน

Books to Scrape is a website built specifically for web scraping practice.

1️⃣ Step 1. Get Web Page

ในขั้นแรก เราจะโหลดหน้าเว็บเข้ามาใน Python ก่อน:

# Load the package
import gazpacho as gz
# Set the URL
url = "<https://books.toscrape.com/catalogue/sapiens-a-brief-history-of-humankind_996/index.html>"
# Load the web page
page = gz.get(url)

และตรวจดูข้อมูลที่โหลดมาได้:

# Inspect the page
print(page[:1000])

ผลลัพธ์:

<!DOCTYPE html>
<!--[if lt IE 7]> <html lang="en-us" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html lang="en-us" class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html lang="en-us" class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en-us" class="no-js"> <!--<![endif]-->
<head>
<title>
Sapiens: A Brief History of Humankind | Books to Scrape - Sandbox
</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="created" content="24th Jun 2016 09:29" />
<meta name="description" content="
From a renowned historian comes a groundbreaking narrative of humanity’s creation and evolution—a #1 international bestseller—that explores the ways in which biology and history have defined us and enhanced our understanding of what it means to be “human.”One hundred thousand years ago, at least six different species of humans inhabited Earth. Yet today there

จะเห็นว่า ข้อมูลที่ได้มาจะอยู่ใน HTML tag


2️⃣ Step 2. Create Soup Object

ในขั้นที่ 2 เราจะสร้าง Soup object ซึ่งเป็น object ที่เราสามารถค้นหาข้อมูลได้โดยง่าย:

# Create a Soup object
soup = gz.Soup(page)

3️⃣ Step 3. Extract Content

ในขั้นสุดท้าย เราสามารถดึงข้อมูลที่ต้องการจาก Soup object ได้ด้วย:

  • .find(): ค้นหา HTML tag ที่ต้องการ
  • .text: ดึงข้อความที่อยู่ใน HTML tag
  • .attrs: ดึง attribute ของ HTML tag มาในรูปแบบ dictionary

เช่น ดึงชื่อหนังสือ:

# Extract the book title
book_title = soup.find("h1").text
# Print the book title
print(book_title)

ผลลัพธ์:

Sapiens: A Brief History of Humankind

ดึงราคา:

# Extract the book price
book_price = soup.find("p", {"class": "price_color"})[0].text
# Print the book price
print(book_price)

ผลลัพธ์:

£54.23

ดึงรูปหนังสือ:

# Extract the book image path
book_image_path = soup.find("img")[0].attrs["src"]
# Append to the base URL
book_image_url = "<https://books.toscrape.com/>" + book_image_path.replace("../../", "")
# Print the image URL
print(book_image_url)

ผลลัพธ์:

<https://books.toscrape.com/media/cache/ce/5f/ce5f052c65cc963cf4422be096e915c9.jpg>

ซึ่งเมื่อเปิดดู URL เราจะได้รูปหนังสือมาแบบนี้:

A book cover image scraped from Books to Scrape.

💪 สรุปการใช้ Gazpacho

วิธีใช้ Gazpacho สำหรับ web scraping มีอยู่ 3 ขั้นตอน:

ขั้นที่ 1. Get web page

# Import the package
import gazpacho as gz
# Set the URL
url = "<https://books.toscrape.com/catalogue/sapiens-a-brief-history-of-humankind_996/index.html>"
# Load the web page
page = gz.get(url)

ขั้นที่ 2. Create Soup object:

# Create a Soup object
soup = gz.Soup(page)

ขั้นที่ 3. Extract content:

# Extract the book title
book_title = soup.find("h1").text
# Print the book title
print(book_title)

🫵 หลังจบบทความนี้

หลังอ่านบทความนี้แล้ว อย่าลืมไปลองทำ web scraping กันนะครับ:


📃 อ้างอิง

Comments

Leave a Reply