Skip to main content
In this tutorial, you’ll build a fully functional spider from scratch that crawls multiple pages, extracts structured data, and exports the results. We’ll walk through each step, explaining the concepts along the way.

What We’ll Build

We’ll create a spider that scrapes quotes from quotes.toscrape.com, a website designed for practicing web scraping. Our spider will:
  1. Start from the homepage
  2. Extract quotes, authors, and tags from each page
  3. Follow pagination links automatically
  4. Export all data to JSON

Prerequisites

Make sure you have Scrapling installed:

Step 1: Basic Spider Structure

Let’s start with the absolute minimum spider:
quotes_spider.py
Let’s break this down:
  • name: A unique identifier for your spider. Required.
  • start_urls: List of URLs where the spider begins crawling. Required.
  • parse(): The default callback method that processes responses. Must be an async generator.

Running Your Spider

Run the spider:
Run it:
You should see:
  • Log messages showing the spider starting and finishing
  • The scraped item: [{'url': 'https://quotes.toscrape.com'}]
  • Statistics about the crawl

Step 2: Extracting Data

Now let’s extract actual quote data. First, inspect the HTML structure:
Update the parse() method:
quotes_spider.py

Understanding Selectors

  • response.css('div.quote'): Finds all <div class="quote"> elements
  • ::text: CSS pseudo-element that extracts text content
  • .get(): Returns the first match (or None)
  • .getall(): Returns all matches as a list
Run the spider again. You should now see structured quote data!

Step 3: Following Pagination

The website has multiple pages. Let’s make the spider follow the “Next” button:
quotes_spider.py
Key points:
  • response.follow(): Creates a new request from a relative or absolute URL
  • callback=self.parse: Tells the spider to process the response with parse()
  • The spider automatically handles relative URLs (like /page/2/)
Run it again - you should now scrape all 100 quotes across 10 pages!

Step 4: Extracting Author Details

Each author name is a link to their detail page. Let’s follow those links and extract more information:
quotes_spider.py
New concepts:
  • meta={'quote_data': quote_data}: Passes data between callbacks
  • response.request.meta['quote_data']: Retrieves the passed data
  • Multiple callbacks: parse() for quotes, parse_author() for author pages

Step 5: Adding Configuration

Let’s add some spider configuration to control crawling behavior:
quotes_spider.py

Step 6: Data Processing & Export

Add data cleaning and export functionality:
quotes_spider.py

Step 7: Adding Pause & Resume

For long-running crawls, enable checkpointing:
quotes_spider.py
Now you can press Ctrl+C while the spider is running. It will pause gracefully and save a checkpoint. Run the script again, and it will resume from where it stopped!

Step 8: Using Different Fetchers

By default, spiders use HTTP requests. For JavaScript-heavy sites, use browser sessions:
advanced_spider.py

Complete Example

Here’s our final, production-ready spider:
quotes_spider.py
Run it:

Testing Your Spider

Before running on the full site, test with a single URL:
test_spider.py

Next Steps

You now have a solid foundation for building web scrapers with Scrapling! Here’s what to explore next: Happy scraping!