Scrapy is an open-source and collaborative framework for extracting the data you need from websites, fast. It's primarily used for web scraping, but it can also be used for data mining or even monitoring and automated testing. Built on top of Python, Scrapy provides a complete solution for web crawling and data extraction, handling many common tasks like making requests, parsing HTML/XML, and saving data.
Key components of Scrapy's architecture include:
- Scrapy Engine: The core of Scrapy, responsible for controlling the flow of data between all components.
- Scheduler: Receives requests from the Engine and queues them for processing.
- Downloader: Fetches web pages from the internet and sends them to the Engine.
- Spiders: User-defined classes that Scrapy uses to crawl a website and extract structured data (called Items) from its pages.
- Item Pipeline: Processes the Items once they have been extracted by the Spiders. This is where you might validate, clean, or store the scraped data (e.g., in a database, CSV, or JSON file).
- Downloader Middlewares: Hooks into Scrapy's request/response processing between the Engine and Downloader. They can modify requests before they are sent, or responses before they are processed by the spider. Examples include handling cookies, user agents, or retries.
- Spider Middlewares: Hooks into Scrapy's spider input/output processing between the Engine and Spiders. They can process spider input (responses) and output (items and requests).
Scrapy operates asynchronously, allowing it to handle many requests concurrently without blocking, making it highly efficient for large-scale scraping projects. It leverages Twisted, an event-driven networking engine, for this purpose.
Advantages of Scrapy:
- Fast and efficient: Asynchronous processing allows for high performance.
- Robust and extensible: Highly customizable with middlewares and pipelines.
- Built-in features: Handles session management, request throttling, proxy rotation, and more.
- Well-structured: Promotes clean and organized scraping code through its component-based architecture.
- Large community: Active development and support.
Use Cases:
- Data aggregation: Collecting data from multiple sources for analysis.
- E-commerce price monitoring: Tracking competitor prices.
- Content discovery: Building search engines or news aggregators.
- Market research: Gathering public data for business intelligence.
Example Code
Let's create a simple Scrapy project to scrape quotes from `quotes.toscrape.com`.
First, you'd typically start a Scrapy project from your terminal:
bash
scrapy startproject quotes_scraper
cd quotes_scraper
Then, you define an `Item` in `quotes_scraper/items.py` (this file is automatically created by `startproject`):
python
import scrapy
class QuotesScraperItem(scrapy.Item):
define the fields for your item here like:
text = scrapy.Field()
author = scrapy.Field()
tags = scrapy.Field()
Next, you create a spider in `quotes_scraper/spiders/quote_spider.py` (or use `scrapy genspider quotes quotes.toscrape.com`):
python
import scrapy
from quotes_scraper.items import QuotesScraperItem
class QuoteSpider(scrapy.Spider):
name = 'quotes'
start_urls = ['http://quotes.toscrape.com/']
def parse(self, response):
Select all div elements with class 'quote'
for quote_div in response.css('div.quote'):
item = QuotesScraperItem()
item['text'] = quote_div.css('span.text::text').get()
item['author'] = quote_div.css('small.author::text').get()
item['tags'] = quote_div.css('div.tags a.tag::text').getall()
yield item
Follow pagination link
next_page = response.css('li.next a::attr(href)').get()
if next_page is not None:
Use response.follow for convenience, it builds an absolute URL
and returns a Request object that will be scheduled.
yield response.follow(next_page, self.parse)
To run this spider and save the output to a JSON file from your project root directory (`quotes_scraper`):
bash
scrapy crawl quotes -o quotes.json
This command would navigate to `quotes.toscrape.com`, extract the text, author, and tags for each quote, follow the "Next" page link, and continue until there are no more pages, saving all extracted data into `quotes.json`.








Scrapy