Python Scrapy 库

Spider/item/pipeline structure, and when Scrapy beats a requests loop.

Intermediate
Key PointsScrapy shines when you crawl many pages with dedupe, throttling and pipelines — for one page, requests is simpler.

Project Layout

Scrapy already handles scheduling, dedupe, throttling, retries and concurrency — you write what to crawl and how to parse.

Bash
pip install scrapy
scrapy startproject books
cd books && scrapy genspider list example.com
Text
books/
├── scrapy.cfg
└── books/
    ├── settings.py
    ├── items.py
    ├── pipelines.py
    └── spiders/list.py

A Spider

parse yields either data (dict/Item) or new Requests (pagination). The framework feeds data to pipelines.

Python
import scrapy


class ListSpider(scrapy.Spider):
    name = "list"
    start_urls = ["https://example.com/books?page=1"]

    def parse(self, response):
        for card in response.css("article.book"):
            yield {
                "title": card.css("h3::text").get(default="").strip(),
                "price": card.css(".price::text").get(default="").strip(),
            }

        next_page = response.css("a.next::attr(href)").get()
        if next_page:
            yield response.follow(next_page, self.parse)
Bash
scrapy crawl list -O books.json      # 直接输出 JSON
Output
{"title": "Python 编程", "price": "39.90"},
{"title": "Go 语言实战", "price": "45.00"},
{"title": "Rust 程序设计", "price": "49.00"}

Politeness Settings

The default concurrency is 16 — unfriendly. AutoThrottle adapts to response time and beats a hard-coded delay.

Python
# settings.py
ROBOTSTXT_OBEY = True
CONCURRENT_REQUESTS_PER_DOMAIN = 4
AUTOTHROTTLE_ENABLED = True
AUTOTHROTTLE_START_DELAY = 1.0
AUTOTHROTTLE_MAX_DELAY = 10.0
ROBOTSTXT_OBEY = True is the default — do not disable it to save effort.

Friends & Partners