Sub topic 7
The crawl frontier: deciding what to fetch next
This chapter assumes you know how HTTP requests and status codes work, can read basic HTML, and can follow short Python snippets. It builds on URL discovery and robots.txt, which feed URLs into the frontier.
What the frontier is
The crawl frontier is the set of URLs a crawler has discovered but not yet fetched. Every fetched page yields new links, and those links go back into the frontier. The crawler repeatedly takes one URL out, fetches it, and adds the new URLs it finds.
Two structures work together:
- The queue holds URLs waiting to be fetched, in a chosen order.
- The seen set records every URL already queued or fetched, so no URL is added twice.
A breadth-first frontier
A first-in, first-out queue gives breadth-first order: all links found on the seed page are fetched before links found on those pages. The example below is a small version of this idea. It uses a deque as the queue and a set as the seen set.
from collections import deque
from urllib.parse import urljoin, urldefrag
def enqueue_links(page_url, hrefs, frontier, seen):
for href in hrefs:
absolute, _ = urldefrag(urljoin(page_url, href))
if absolute not in seen:
seen.add(absolute)
frontier.append(absolute)
frontier = deque(["https://example.com/"])
seen = {"https://example.com/"}
enqueue_links(
"https://example.com/",
["/about", "/about#team", "blog/", "https://example.com/"],
frontier,
seen,
)
print(list(frontier))This prints ['https://example.com/', 'https://example.com/about', 'https://example.com/blog/']. The fragment #team was dropped, so the second /about counted as a duplicate. The seed was already seen, so it was not added again.
Prioritization
A plain queue treats every URL equally. Real crawlers use a priority queue, because they can fetch only a fraction of what they find. Common signals include:
- Link depth: pages close to the seed are often more important.
- Popularity: URLs with many inbound links are fetched sooner.
- Change rate: pages that changed often in the past are revisited sooner.
- Sitemap hints: a
lastmoddate can raise a URL's priority.
Production frontiers usually keep a separate queue per host. This lets the crawler pick the next URL by importance while also respecting per-host rate limits, which the next chapter covers.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Crawler never finishes | Infinite URL spaces such as calendars or session IDs in the query string | Cap depth, limit URLs per host, normalize or drop tracking parameters |
| Same page fetched many times | URLs not normalized before the seen check | Resolve relative links, strip fragments, lowercase the host |
| One site gets hammered | A single global queue puts one host's URLs next to each other | Use per-host queues with delays |
| Blocked URLs waste effort | robots.txt checked only after fetching | Check permission before a URL leaves the frontier |
Exercise
Exercise
A crawler uses a FIFO queue and a seen set. Page A links to B and C. Page B links to D. Page C links to D and A. In what order are A, B, C, and D fetched, and how many times is D added to the frontier?
Show answer
The order is A, B, C, D. A is fetched first, then B and C are queued. B is fetched next and adds D. C is fetched and finds D and A, but both are already in the seen set. D is added to the frontier exactly once.
Exercise
Why does a crawler check the seen set when it adds a URL to the frontier instead of only when it fetches the URL?
Show answer
Checking at insertion keeps duplicates out of the queue. Otherwise the queue would fill with repeated entries, using memory and making ordering and prioritization harder.