# Crawl a site: one call with a page limit, and what it returned

Turn the one-page request into a single crawl call with a page limit written in the script, then read the dated list of 20 pages it returned.

Canonical: https://spider.cloud/learn/course/crawl-a-site/

To crawl a website, fetch one start page, follow its links to more pages on the same site, and stop at a page limit you chose before the first request. When an API does the link following for you, the whole crawl is one call that carries the start address and that limit.

At the end of this lesson you have `crawl.py`, which sends that one call with a limit of 20 fixed in the file, and a list of the pages it got back, each with its status. On the practice site the list held 20 addresses, all of them 200.

## From one page to many

Google's overview of its own crawlers describes a crawler, in Google's words, as "a generic term for any program that is used to automatically discover and scan websites". The discovering is the part [lesson 6](/learn/course/list-the-links/) did by hand: it read one page and sorted its links. Lesson 6's script stopped there and fetched none of them. Crawling is the step of [web scraping](/web-scraping/) that goes on to fetch those links, and their links, until something tells it to stop.

That something has to be a number you set. The API reference says this of the `limit` field on a crawl: "Maximum pages to crawl per site. Unset or 0 crawls every page found". A crawl body with no limit is a crawl of the whole site.

## What the script changes

`crawl.py` keeps the key check and the standard-library request from the earlier lessons. Four things are new. `API` now points at `/crawl` instead of `/scrape`. The body carries `"limit": 20`, typed into the file and not read from the command line, so a slip of the keyboard cannot start a crawl of thousands of pages. An optional second argument sets `depth`, which the next lesson uses. And because a crawl returns many pages, the end of `main()` now loops over them, prints one status and address per page, and exits 1 if any page answered something other than 200.

The script reads your API key from the `SPIDER_API_KEY` environment variable. Here it is exactly as it ran on 2026-11-20.

```python
"""Crawl a site through the Spider API, at most 20 pages, and list each page with its status.

    python3 crawl.py URL [DEPTH]

The page limit is written in the file, so a typo on the command line cannot start a large crawl.
DEPTH, when given, is how many links away from URL the crawl may go.
Exit 0: every page answered 200.
Exit 1: the key is missing, nothing came back, or a page answered something else.
"""
import json
import os
import sys
import urllib.request

API = "https://api.spider.cloud/crawl"


def main() -> int:
    if len(sys.argv) not in (2, 3):
        print("usage: python3 crawl.py URL [DEPTH]")
        return 1
    key = os.environ.get("SPIDER_API_KEY")
    if not key:
        print("set SPIDER_API_KEY first, as in lesson 1")
        return 1

    params = {"url": sys.argv[1], "limit": 20, "request": "http", "return_format": "markdown"}
    if len(sys.argv) == 3:
        params["depth"] = int(sys.argv[2])
    req = urllib.request.Request(API, data=json.dumps(params).encode(), headers={
        "Authorization": f"Bearer {key}",
        "Content-Type": "application/json",
    })
    with urllib.request.urlopen(req, timeout=300) as resp:
        pages = json.load(resp)
    if isinstance(pages, dict):
        pages = [pages]

    print(f"asked: limit {params['limit']}" + (f", depth {params['depth']}" if "depth" in params else ""))
    print(f"pages: {len(pages)}")
    bad = 0
    for page in sorted(pages, key=lambda p: p.get("url") or ""):
        status = page.get("status")
        bad += status != 200
        print(f"  {status}  {page.get('url')}")
    return 0 if pages and not bad else 1


if __name__ == "__main__":
    sys.exit(main())
```

The recorded run sent no `user_agent` and no `delay`. Before you run it, replace the `params = ...` line with these two, putting your bot's name and contact where the example ones are.

```python
    params = {"url": sys.argv[1], "limit": 20, "request": "http", "return_format": "markdown",
              "user_agent": "examplebot/1.0 (+https://example.com/bot; bot@example.com)", "delay": 1000}
```

The reference gives `delay` as "Milliseconds to wait between requests, up to 60000", so 1000 is one second between pages. [Lesson 4](/learn/course/pace-your-requests/) is where that pace comes from, and your run will take longer than the recorded one because of it.

## Crawl the practice site

```bash
python3 crawl.py https://quotes.toscrape.com/
```

On 2026-11-20 this command made one API call under Python 3.14.7, finished in 5.3 seconds, exited 0 and printed the lines below.

```text
asked: limit 20
pages: 20
  200  http://quotes.toscrape.com/author/Eleanor-Roosevelt/
  200  http://quotes.toscrape.com/author/Marilyn-Monroe/
  200  http://quotes.toscrape.com/author/Steve-Martin/
  200  https://quotes.toscrape.com/
  200  https://quotes.toscrape.com/page/2/
  200  https://quotes.toscrape.com/tag/adulthood/page/1/
  200  https://quotes.toscrape.com/tag/books/
  200  https://quotes.toscrape.com/tag/classic/page/1/
  200  https://quotes.toscrape.com/tag/deep-thoughts/page/1/
  200  https://quotes.toscrape.com/tag/edison/page/1/
  200  https://quotes.toscrape.com/tag/friendship/
  200  https://quotes.toscrape.com/tag/live/page/1/
  200  https://quotes.toscrape.com/tag/love/
  200  https://quotes.toscrape.com/tag/love/page/1/
  200  https://quotes.toscrape.com/tag/miracles/page/1/
  200  https://quotes.toscrape.com/tag/obvious/page/1/
  200  https://quotes.toscrape.com/tag/simile/page/1/
  200  https://quotes.toscrape.com/tag/success/page/1/
  200  https://quotes.toscrape.com/tag/value/page/1/
  200  https://quotes.toscrape.com/tag/world/page/1/
```

This clip sorts the recorded list.

One crawl call, and what it returns. 1 min 8 s, no sound. The words are on screen, and the captions button shows them at your own text size.

Read the transcript

One crawl call, and what it returns. A start address and a page limit, then a list of pages.

The script asked for 20 pages. 20 came back, and each answered 200. asked: limit 20. pages: 20. answered 200: 20 of 20. exit code: 0.

Sort the list by kind. Tag pages took 15 of the 20 places. 15 under /tag/. 3 under /author/. 1 the front page, /. 1 under /page/. 4: kinds of page in the list. 15: of 20 places went to /tag/.

Two addresses may be one page. Each used a place out of the 20. /tag/love/ and /tag/love/page/1/ are both in the list, each in a place of its own. Beside them in the sorted list: /tag/live/page/1/ and /tag/miracles/page/1/. 16 more addresses are counted, not shown. 2: places, maybe for one page. 20: places in all.

3 came back as http\://. Keep each address as returned. http\://: 3 pages, all under /author/. https\://: 17 pages. host: quotes.toscrape.com. on another host: 0 pages.

Set the limit before the first request. Without one, a crawl fetches every page found. The page limit: "limit": 20. limit: At most that many pages. unset: Every page found. 0: The same as unset. This run: limit.

Read the full lesson. The page is at spider.cloud/learn/course/crawl-a-site.

## Read the list

Start with the two counts. The file asked for 20 and 20 came back. Lesson 6 found 47 distinct on-site addresses on the front page alone, so the site had more pages than this crawl took, and the count matches the number you typed into the file.

Then sort the 20 by kind. One is the front page, one is the second listing page, three are author pages and fifteen are tag pages. The list is in alphabetical order because the script sorts it, so it says nothing about which page was fetched first.

Two lines are the likely pair lesson 6 warned about: `/tag/love/` and `/tag/love/page/1/`. Both are here, so each one used up a place out of the 20. The run does not tell you whether they hold the same quotes.

The three author pages stand out. They came back as `http://` addresses with a slash at the end. The other seventeen are `https://`, and lesson 6's link list wrote the same author pages as `https://` with no slash. The address on each line is the one that page was finally served from, not necessarily the link that led to it. This run does not show why the site serves those three that way, so keep the address as listed and do not rewrite it. The `http://` lines sort to the top because `http:` comes before `https:` in alphabetical order.

Every address is on `quotes.toscrape.com`. None of the 20 is on another host, though lesson 6 counted two links that point elsewhere. Before you aim the script at a site you do not own, check its robots.txt the way [lesson 3](/learn/course/read-robots-txt/) did, and read the site's terms.

## Try it yourself

Open `crawl.py`, change `"limit": 20` to `"limit": 5`, keep the `user_agent` and `delay` you added, and crawl the same practice site again, then print the exit code:

```bash
python3 crawl.py https://quotes.toscrape.com/
echo $?
```

The run above cannot tell you which five you will get, so do not expect them to be the first five lines of its list. Check each address you get against the site in a browser. You are done when the first line reads `asked: limit 5`, the `pages:` line shows 5 or fewer, every line below it starts with 200, and `echo $?` prints 0.

## Do this, avoid this

### Do this

- Write the page limit into the script, so the command line cannot change it.
- Start a new crawl with a small limit, read the whole list, then raise it.
- Compare the pages count with the limit after every run.
- Keep each address exactly as the crawl returned it when you save or compare results.

### Avoid this

- Sending a crawl body with no limit, which the reference says crawls every page found.
- Rewriting the http\:// lines as https\:// to make the list look tidy.
- Reading the sorted list as the order the pages were fetched.
- Taking 20 pages that answered 200 as the whole site.

## Next

All 20 pages came from a crawl with no depth set, and the reference gives the default as "Maximum crawl depth. Default 25. 0 removes the limit." The next lesson asks how far from the front page a crawl should be allowed to go, using the `DEPTH` argument `crawl.py` already accepts.

## Sources

1. [Google for Developers, Overview of Google crawlers and fetchers (user agents)](https://developers.google.com/crawling/docs/crawlers-fetchers/overview-google-crawlers) Read November 21, 2026
2. [Spider OpenAPI file, the limit, depth, delay and user_agent parameters of /crawl](https://spider.cloud/openapi.yaml) Read November 21, 2026
