การดึงข้อมูลแบบกระจายด้วย Scrapy
เชี่ยวชาญ Scrapy เฟรมเวิร์ก Python ทรงพลังสำหรับสร้างโปรแกรมรวบรวมและดึงข้อมูลจากเว็บไซต์ขนาดใหญ่แบบกระจาย
การดึงข้อมูลแบบกระจายด้วย Scrapy เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Scraping & Bots และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Scrapy?
Scrapy is a powerful and fast open-source web crawling and web scraping framework for Python. It's designed to make building large-scale data extraction projects much easier and more efficient.
Think of it as a complete ecosystem for fetching web pages, parsing their content, and saving the extracted data.
Why Use Scrapy?
Scrapy offers several advantages, especially for complex or large-scale scraping tasks:
- Asynchronous Processing: Handles requests concurrently, making it very fast.
- Built-in Tools: Provides robust mechanisms for parsing HTML/XML using CSS selectors and XPath.
- Extensibility: Easily customize behavior with middleware and pipelines.
- Distributed Ready: Designed with an architecture that can be scaled across multiple machines.
Scrapy Project Structure
When you start a new Scrapy project, it sets up a standard directory structure to organize your files. This structure helps keep your scraping logic neat and maintainable.
You typically create a project using the command: scrapy startproject myproject
myproject/spiders/: Contains your spider files.myproject/items.py: Defines the data you want to scrape.myproject/pipelines.py: Processes scraped items.myproject/settings.py: Configures project-wide settings.
Spiders: The Core Crawlers
Spiders are classes that you define to tell Scrapy how to crawl a site and extract data. Each spider has a unique name and defines a starting point (or points) for crawling.
The main parts of a spider are:
name: A unique identifier for your spider.start_urls: A list of URLs where the spider will begin crawling.parse()method: This method is called for each downloaded response from thestart_urls. It's where you'll write your data extraction logic.
Building a Basic Spider
Let's look at a simple spider definition. This spider is designed to fetch content from a single URL and print a confirmation. In a real scenario, the parse method would contain detailed extraction logic.
import scrapy
class MyFirstSpider(scrapy.Spider):
name = 'first_spider'
start_urls = ['http://quotes.toscrape.com/']
def parse(self, response):
# This method is called for each page in start_urls
# For now, we'll just log that a page was parsed.
# In a real spider, you'd extract data here.
self.log(f'Visited {response.url}')
Items: Structuring Your Data
Items are simple classes that help you define the structure of the data you want to scrape. They are similar to dictionaries but offer more structure and are easier to work with.
By defining an Item, you clearly state what fields (like 'title', 'author', 'price') you expect to extract from a web page.
import scrapy
class QuoteItem(scrapy.Item):
# Define the fields for your item here
text = scrapy.Field()
author = scrapy.Field()
tags = scrapy.Field()
# In your spider, you would create instances of this Item
# and populate its fields with extracted data.
Item Pipelines: Data Processing
Item Pipelines are components that process an Item once it has been scraped by a spider. They are incredibly useful for various tasks:
- Cleaning HTML tags or unwanted characters.
- Validating extracted data.
- Storing the item in a database (SQL, NoSQL).
- Saving items to files (CSV, JSON).
You activate pipelines in your project's settings.py file.
class MyDataPipeline:
def process_item(self, item, spider):
# Example: Convert author's name to uppercase
if 'author' in item:
item['author'] = item['author'].upper()
return item
# In settings.py, you would enable this:
# ITEM_PIPELINES = {
# 'myproject.pipelines.MyDataPipeline': 300,
# }
Selectors for Data Extraction
Inside your spider's parse() method, Scrapy provides powerful tools for extracting specific data: CSS selectors and XPath expressions.
- CSS Selectors: Use syntax similar to what you'd find in CSS stylesheets (e.g.,
.quote-text::text). - XPath: A query language for selecting nodes in an XML or HTML document (e.g.,
//span[@class='author']/text()).
Both are highly effective for pinpointing elements and extracting their content or attributes.
Running a Scrapy Spider
After defining your spider and setting up your project, you run it from the command line. Navigate to your project's root directory (where scrapy.cfg is located).
The command scrapy crawl <spider_name> starts the scraping process. You can also specify output formats.
# To run your spider named 'my_first_spider':
# Open your terminal and navigate to your project folder.
# Type the following command:
# scrapy crawl my_first_spider
# To save the output to a JSON file:
# scrapy crawl my_first_spider -o output.json
Scrapy's Scalability
Scrapy's architecture makes it inherently suitable for distributed scraping. Its asynchronous design allows it to handle many requests efficiently, and components like the Scheduler and Downloader can be configured for distributed setups.
While full distributed deployment often involves additional tools (like Scrapy-Redis or message queues), Scrapy provides the foundational framework for building highly scalable web scraping solutions.
Scrapy Components Quiz
Which of the following are fundamental components you would typically define or configure within a Scrapy project?
Scrapy Recap
In this lesson, we introduced Scrapy, a powerful Python framework for web scraping. We covered:
- The benefits of using Scrapy for scalable projects.
- The standard project structure and its key components.
- How to define a Spider to crawl web pages.
- The role of Items for structuring scraped data.
- The function of Item Pipelines for processing data.
- How Scrapy's design supports distributed scraping.
Scrapy empowers you to build robust and efficient data collection systems for almost any web scraping challenge.
คำถามที่พบบ่อย
บทเรียน “การดึงข้อมูลแบบกระจายด้วย Scrapy” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การดึงข้อมูลแบบกระจายด้วย Scrapy” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การดึงข้อมูลแบบกระจายด้วย Scrapy”
เชี่ยวชาญ Scrapy เฟรมเวิร์ก Python ทรงพลังสำหรับสร้างโปรแกรมรวบรวมและดึงข้อมูลจากเว็บไซต์ขนาดใหญ่แบบกระจาย คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การดึงข้อมูลแบบกระจายด้วย Scrapy” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Scraping & Bots นี้ได้ไหม
ได้ บทเรียน Web Scraping & Bots ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การดึงข้อมูลแบบกระจายด้วย Scrapy
- Cloud Functions สำหรับการดึงข้อมูล
- การตรวจติดตามและการบันทึกการทำงาน
- การกระจายงานด้วยคิว