Scrapy
A full crawling framework — scheduling, concurrency, retries, pipelines and throttling included.
What it is
Scrapy is a fast, open-source web crawling and web scraping framework for Python. It provides tools to extract data from websites, process it, and store it in formats like JSON, CSV, or databases.
Scrapy allows you to define spiders that navigate through websites, extract information using selectors or XPath/CSS expressions, and export the collected data. It includes support for requests, middleware, pipelines, and asynchronous networking for high performance.
- Licence
- BSD 3-clause
- Watch for
- Respect robots.txt and rate limits; politeness settings exist for a reason
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Crawling many pages or whole sites, not just fetching a few
- You need request throttling, retries, deduplication and structured output pipelines
Look elsewhere when
- You just need to grab data from one page — Requests plus Beautiful Soup is far less setup
- The target renders content with JavaScript, unless you add a browser middleware
Installation
pip install scrapyGetting started
The smallest useful thing you can do with it, and what each part means.
# In terminal:
scrapy startproject myprojectimport scrapy
class QuotesSpider(scrapy.Spider):
name = 'quotes'
start_urls = ['http://quotes.toscrape.com']
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').get(),
'author': quote.css('small.author::text').get(),
}Advanced usage
Where the library earns its place over a simpler alternative.
# Terminal command:
scrapy crawl quotes -o quotes.jsonclass QuotesPipeline:
def process_item(self, item, spider):
item['text'] = item['text'].strip()
return itemdef parse(self, response):
for quote in response.css('div.quote'):
yield {...}
next_page = response.css('li.next a::attr(href)').get()
if next_page:
yield response.follow(next_page, self.parse)response.xpath('//div[@class="quote"]/span[@class="text"]/text()').getall()Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- TwistedError: DNS lookup failed
- Check your network connection or domain name validity.
- HttpError
- Use proper exception handling with Scrapy middleware or retry requests.
- ValueError: no JSON object could be decoded
- Ensure the response body is correctly formatted before parsing.
Best practices
- Use pipelines for cleaning, validating, and storing scraped data.
- Leverage middlewares for handling retries, user agents, and proxies.
- Use asynchronous requests to speed up crawling large sites.
- Respect `robots.txt` and website terms of service.
- Organize multiple spiders logically within a Scrapy project.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Scrapy was created by Pablo Hoffman and released in 2008. It was designed to provide a framework for web scraping that is fast, extensible, and reliable. Scrapy has become popular in both industry and research for automated data extraction, web crawling, and building data pipelines.
