Glossary
Scrapy: a framework for crawling at scale
Scrapy is an open source Python framework for crawling websites and extracting structured data, built around spiders, an asynchronous engine and a chain of middlewares.
How a Scrapy project is organized
Scrapy gives you structure where a script would give you freedom. That structure pays off once a crawl has many pages, retries and output formats.
- Spiders define where to start and how to parse each response, yielding items and new requests.
- The engine and scheduler manage the request queue, deduplicate URLs and run many downloads at once on the Twisted event loop.
- Downloader middlewares sit between the engine and the network and can change requests and responses: add headers, rotate proxies, retry, detect blocks.
- Item pipelines clean, validate and store each item.
- Feed exports write JSON, CSV or XML without extra code.
A small spider
import scrapy
class ShelfSpider(scrapy.Spider):
name = "shelf"
start_urls = ["https://books.toscrape.com/"]
def parse(self, response, **kwargs):
for card in response.css("article.product_pod"):
yield {
"title": card.css("h3 a::attr(title)").get(),
"price": card.css(".price_color::text").get(),
}
nxt = response.css("li.next a::attr(href)").get()
if nxt:
yield response.follow(nxt, callback=self.parse)
Run it with scrapy runspider shelf.py -O books.json and the feed export does the rest.
Settings worth knowing
ROBOTSTXT_OBEY makes Scrapy read robots.txt. CONCURRENT_REQUESTS_PER_DOMAIN and DOWNLOAD_DELAY control pressure on each site. AUTOTHROTTLE_ENABLED adapts the delay to server response times. RETRY_HTTP_CODES decides which statuses are retried. Starting with conservative values and Auto Throttle on keeps most crawls below the level that triggers bot checks.
JavaScript pages
Scrapy downloads raw HTML and does not run scripts. For pages that build content in the browser, either call the underlying JSON endpoint, or add a rendering plugin such as scrapy-playwright that hands selected requests to a real browser. Check the plugin's current documentation for setup, as the integration details change between versions.
Where a CAPTCHA step fits
A downloader middleware is the natural place. In process_response, detect a challenge (a 403 or 503 with a challenge script, or a widget in the HTML), read the site key, create a task, and when the token or cookie returns, reissue the request with it attached. Because Scrapy is asynchronous, avoid blocking calls like time.sleep inside the middleware; use an async HTTP client for the GateSolver calls or run them in a thread. A reCAPTCHA v2 token usually takes under 9 seconds, and a failed task is not billed. The Scrapy CAPTCHA middleware article and the Scrapy integration guide show complete code.
Spotting blocks early
A crawl that silently stores challenge pages as data is worse than one that stops. Add a check in your spider or a middleware that flags responses with unusually short bodies, a title such as "Just a moment...", or a missing element that every real page has. Count these per domain in the crawl stats, so a rising number tells you a site changed its protection before the dataset fills with empty items.
When Scrapy is the right tool
Choose it for crawls with many pages and a stable structure, where you want retries, throttling, deduplication and exports without writing them yourself. For a one off page or quick exploration, Requests plus Beautiful Soup is faster to start.
Common questions
Does Scrapy run JavaScript?
No. It downloads raw responses. Use a browser plugin such as scrapy-playwright or call the JSON endpoint behind the page.
How do I make Scrapy respect robots.txt?
Set ROBOTSTXT_OBEY to True in the project settings; new projects created with the CLI enable it by default.
Where should CAPTCHA handling live in Scrapy?
In a downloader middleware that detects the challenge, gets the answer and retries the request with it.
Can I use time.sleep inside a Scrapy middleware?
Avoid it. It blocks the whole engine; use non blocking waits or run the polling elsewhere.
What is Auto Throttle?
A Scrapy extension that adjusts download delay to server latency so the crawl slows down when a site is under load.
Keep reading
- ScrapyA settings-driven Scrapy downloader middleware that solves only on requests you mark, with a shared HTTP session and crawl stats.
- Handle CAPTCHAs in a Scrapy middlewareAn async downloader middleware that spots a challenge response, solves it through the API and reschedules the request, keeping spiders clean.
- Scrapy vs Beautiful SoupA crawling framework and an HTML parser are different tools: what each handles, when to pick which, and how to combine them.
- Beautiful SoupThe forgiving Python library for parsing HTML and XML into a tree you can search by tag, attribute or CSS selector.