# Set the depth: crawl at depth 1 and 2 and compare the pages

Run the lesson 7 crawl at depth 1 and at depth 2 on a practice site, count the pages each returned, and see why depth counts path segments in the address.

Canonical: https://spider.cloud/learn/course/set-the-depth/

When a crawl starts at a site's front page, its depth setting caps how many path segments an address may have, the slash-separated parts after the host, so depth 1 keeps the front page and addresses like `/login` and drops `/tag/love/`. It does not count how many links the crawler followed to get somewhere.

When you finish this lesson you will have two measured page counts for the same practice site, 2 at depth 1 and 20 at depth 2, and the 18 addresses the depth 2 crawl returned and depth 1 did not. You will also be able to predict, before you send a crawl, which addresses a given depth lets in.

## Where the script stands

[Lesson 7](/learn/course/crawl-a-site/) left you with `crawl.py`, which sends one crawl call with a limit of 20 written into the file. This lesson adds no lines to it. It already takes an optional second argument, and when you pass one, the script puts it in the body as `depth`. So what changes is the command you type.

The API reference gives the whole definition of the field as "Maximum crawl depth. Default 25. 0 removes the limit." Lesson 7's crawl sent no depth, so it ran at 25.

## The script, as it ran

Your API key comes from the `SPIDER_API_KEY` environment variable, as in the crawl lesson. This is the file exactly as all three runs on 2026-11-20 used it.

```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())
```

One line of that docstring is wrong. It calls DEPTH the number of links away from the start the crawl may go, and the crawler's source, quoted further down, shows it is not. Fix the comment in your copy once you have read why.

The saved runs went out without a `user_agent` or a `delay`. Lesson 7's exercise also left your copy at `"limit": 5`, and at 5 the depth 2 run cannot match the 20 recorded below. Before the first command, make the `params` line read as follows, with your own bot name and contact, so the limit is back at 20 and your `user_agent` and `delay` stay in:

```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 `delay` of 1000 puts one second between requests to the site, so expect your runs to take longer than the recorded ones.

## Crawl at depth 1

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

On 2026-11-20, under Python 3.14.7, that command exited 0 and printed these four lines.

```text
asked: limit 20, depth 1
pages: 2
  200  https://quotes.toscrape.com/
  200  https://quotes.toscrape.com/login
```

Two pages from a limit of 20. A second run of the same command on the same day printed the same two addresses, so the small count was not a one-off.

## Crawl at depth 2

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

The depth 2 run, also made on 2026-11-20, exited 0 with this list.

```text
asked: limit 20, depth 2
pages: 20
  200  http://quotes.toscrape.com/author/Albert-Einstein/
  200  http://quotes.toscrape.com/author/Andre-Gide/
  200  http://quotes.toscrape.com/author/Eleanor-Roosevelt/
  200  http://quotes.toscrape.com/author/J-K-Rowling/
  200  http://quotes.toscrape.com/author/Jane-Austen/
  200  http://quotes.toscrape.com/author/Marilyn-Monroe/
  200  http://quotes.toscrape.com/author/Steve-Martin/
  200  http://quotes.toscrape.com/author/Thomas-A-Edison/
  200  https://quotes.toscrape.com/
  200  https://quotes.toscrape.com/login
  200  https://quotes.toscrape.com/page/2/
  200  https://quotes.toscrape.com/tag/books/
  200  https://quotes.toscrape.com/tag/friends/
  200  https://quotes.toscrape.com/tag/humor/
  200  https://quotes.toscrape.com/tag/inspirational/
  200  https://quotes.toscrape.com/tag/life/
  200  https://quotes.toscrape.com/tag/love/
  200  https://quotes.toscrape.com/tag/reading/
  200  https://quotes.toscrape.com/tag/simile/
  200  https://quotes.toscrape.com/tag/truth/
```

Take away the front page and `/login` and 18 lines are left, none of them in the depth 1 list: eight author pages, the second listing page `/page/2/`, and nine tag pages. The count hit the limit of 20 exactly, so this run does not tell you how many pages depth 2 would allow with no limit. The author pages came back as `http://` with a trailing slash, the same quirk lesson 7 found; keep them as printed.

## What depth counts

Look at `/page/2/`. Lesson 6's [list of links](/learn/course/list-the-links/) found it on the front page, one click from the start, and the depth 1 crawl still left it out. The runs alone cannot settle why, since they show only what came back. The crawler's source code settles it, and the two lists fit what it says.

That rule is about the shape of each address. RFC 3986, the IETF standard for address syntax, says "A path consists of a sequence of path segments separated by a slash". Count them:

| Address                    | Path segments | Depth 1  | Depth 2  |
| -------------------------- | ------------- | -------- | -------- |
| `/`                        | 0             | returned | returned |
| `/login`                   | 1             | returned | returned |
| `/page/2/`                 | 2             | left out | returned |
| `/tag/love/`               | 2             | left out | returned |
| `/author/Albert-Einstein/` | 2             | left out | returned |

You can read the rule in the open source [spider crate](https://github.com/spider-rs/spider). The function `is_over_inner_depth_budget` in `spider/src/website.rs` splits the path on `/`, adds one per segment, and marks the address as over budget once the count passes a threshold set when the crawl starts. A start address with a longer path can raise that threshold. Every crawl in this lesson starts at the front page, `/`, where it is the depth you send. A comment in that code says to "Skip empty segments so a trailing slash … counts the same", so `/tag/love/` and `/tag/love` are both two. The check that calls it carries the note "Validate if url exceeds crawl depth and should be ignored."

The lesson 7 crawl ran at the default of 25. Its list held addresses such as `/tag/love/page/1/`, which has four segments. Neither depth 1 nor depth 2 could return one of those, and the depth 2 list above has none.

So set depth from the site's address layout, not from how far you think a page sits from the front. On this site, depth 2 permits any address of two segments, such as `/tag/friendship/`, which lesson 6 found on the front page, but the run stopped at 20 pages and that one is not on its list. What the run returned is eight author pages, nine tag pages and `/page/2/`; which other two-segment pages a larger limit would add, this run does not show. A paged tag list such as `/tag/love/page/1/` is over the depth either way. Picking a depth is picking which kinds of page a [web scraping](/web-scraping/) job collects from a site.

## Try it yourself

Run the same site at depth 3, with your `user_agent` and `delay` in place, then print the exit code:

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

Before you look at the output, write down which kinds of address you expect. A paged tag list such as `/tag/love/page/1/` has four segments, so it should not appear. Then count the segments of every address on the list by hand, ignoring a slash at the end. The one-second delay paces the requests, so the run will not finish at once. You are done when the first line reads `asked: limit 20, depth 3`, no address on the list has more than three segments, and `echo $?` prints 0.

A deeper setting lets more addresses in, so with a larger limit it can mean more requests to the site. Before you raise the depth on a site you do not own, read its robots.txt as in [lesson 3](/learn/course/read-robots-txt/), check its terms, and keep the pace from [lesson 4](/learn/course/pace-your-requests/).

## Do this, avoid this

### Do this

- For a crawl from the front page, count the slash-separated parts of an address to know whether a depth will let it in.
- Pick the depth from the address layout of the pages you want, such as two for /tag/name/ pages.
- Run at depth 1 first and read the list before you raise it.
- Check whether the pages count equals the limit, since then the crawl reached its cap and more pages at that depth may remain.

### Avoid this

- Choosing a depth by counting clicks from the front page.
- Expecting a one-click page such as /page/2/ to appear at depth 1.
- Sending depth 0 to mean a shallow crawl, since the reference says 0 removes the limit.
- Copying the docstring's links-away wording into your own notes.

## Next

You now have three crawl lists from the same site, and reading them off the terminal is already slow. The next lesson asks how to write each page a crawl returns into a CSV file you can sort and compare.

## Sources

1. [spider crate source, spider/src/website.rs, the is_over_inner_depth_budget function](https://raw.githubusercontent.com/spider-rs/spider/main/spider/src/website.rs) Read November 21, 2026
2. [RFC 3986, Uniform Resource Identifier (URI): Generic Syntax, IETF, January 2005](https://www.rfc-editor.org/rfc/rfc3986.html) Read November 21, 2026
3. [Spider OpenAPI file, the depth parameter of /crawl](https://spider.cloud/openapi.yaml) Read November 21, 2026
