Sub topic 11
Crawl traps, depth limits, and page budgets
This chapter assumes you have read the chapters on URL normalization, the seen set, and the URL frontier. You should be comfortable reading short Python programs and know how queues and hash sets work.
What a crawl trap is
A crawl trap is a set of URLs that is effectively infinite, or so large that the crawler never leaves it. The seen set does not help, because every URL in the trap is new. Common sources:
- Calendars with "next month" links that continue for thousands of years.
- Session IDs or tracking parameters added to every link, such as
?sid=a81f. - Sort and filter combinations in shops, such as
?color=red&size=m&sort=price. - Relative-link loops, such as
/a/b/a/b/a/b/..., caused by a broken link or a misconfigured server. - Pages that generate a fresh "next" link on demand, such as infinite pagination.
Traps are rarely malicious. Most come from ordinary dynamic sites. The result is the same: the crawler spends its budget on one host and collects almost nothing new.
Three layers of defense
No single check catches every trap, so combine several limits.
| Limit | What it caps | What it catches |
|---|---|---|
| Depth limit | Link hops from a seed | Pagination chains, deep loops |
| URL shape limit | Length, path segments, query parameters | Repeating paths, parameter explosions |
| Page budget | Pages per host or per crawl | Anything the other limits miss |
Implementing the limits
Each frontier entry needs its depth. A link found on a page at depth d has depth d + 1. Seeds have depth 0. The function below applies all three checks.
from collections import Counter
from urllib.parse import urlsplit
MAX_DEPTH = 6
MAX_URL_LENGTH = 200
MAX_PATH_SEGMENTS = 10
MAX_QUERY_PARAMS = 4
MAX_PAGES_PER_HOST = 500
host_counts = Counter()
def shape_ok(url):
parts = urlsplit(url)
if len(url) > MAX_URL_LENGTH:
return False
segments = [s for s in parts.path.split('/') if s]
if len(segments) > MAX_PATH_SEGMENTS:
return False
# The same segment three or more times usually means a relative-link loop.
if segments and max(Counter(segments).values()) >= 3:
return False
params = [p for p in parts.query.split('&') if p]
return len(params) <= MAX_QUERY_PARAMS
def should_enqueue(url, depth):
if depth > MAX_DEPTH:
return False
if not shape_ok(url):
return False
return host_counts[urlsplit(url).netloc] < MAX_PAGES_PER_HOST
print(should_enqueue('https://example.com/a/b/a/b/a/b/a', 3)) # False
print(should_enqueue('https://example.com/blog/post-1', 2)) # TrueIncrement host_counts[host] when you fetch a page, not when you enqueue it. Counting at enqueue time stops the crawl early on hosts with many links that you would never reach.
In the crawl loop, store (url, depth) in the frontier and pass depth + 1 for links found on a page.
Choosing the numbers
- Depth: most useful content sits within 5 to 10 hops of a seed. Pick a value, crawl a sample, and check how many pages you receive at each depth. If the counts keep growing at the limit, you may be cutting off real content or hitting a trap.
- Per-host budget: set it by how much you want from one site. A small budget keeps a trap from dominating the crawl but also truncates large legitimate sites.
- Global budget:
max_pages, as in the crawl loop from earlier chapters, guarantees the crawl ends.
Remaining gotchas
Strip known tracking parameters (utm_*, sid) during normalization. This removes many traps before the limits are needed. Also log every rejected URL with the rule that rejected it. A limit that silently drops real pages is hard to diagnose.
Exercise
A crawler has a per-host budget of 500 pages. It enqueues 40 links from every page it fetches, and counts a page against the budget when it enqueues the link. What goes wrong, and what is the fix?
Show answer
The budget fills after about 13 fetched pages, because each page adds 40 counted links. The crawler then stops collecting from that host with almost nothing downloaded. The fix is to increment the host counter when a page is fetched, not when its link is enqueued.
Exercise
Why is a seen set alone not enough to stop a calendar trap?
Show answer
Each "next month" link is a different URL, so none of them are in the seen set. The set only prevents repeats, and a trap produces endless new URLs. A depth limit, URL shape limit, or page budget is needed to stop it.