36. Design a Web Crawler
1
The interview prompt2
36.1 What do you ask first?3
36.2 How big is this, really?4
36.3 What are the doors?5
36.4 What do we store?6
36.5 What does the whole system look like?7
36.6 How does the URL frontier work?8
36.7 How do politeness and robots.txt work?9
36.8 How do you de-duplicate with Bloom filters?10
36.9 How do you crawl with many machines?11
36.10 What would you say in the last five minutes?12
Score yourself13
Variations to practise14
Sources15
Code lab36. Design a Web Crawler
36.7 How do politeness and robots.txt work?
3 min read+10 XP
robots.txt is a file at the root of a site (/robots.txt) telling crawlers which paths they may fetch. It was standardised as RFC 9309 in 2022:
User-agent: *
Disallow: /cart
Disallow: /search
Allow: /search/about
User-agent: BadBot
Disallow: /
Rules that matter:
- Use the group for your user agent, or
*if none matches. - The longest matching rule wins; if an Allow and a Disallow match equally, Allow wins. So
/search/aboutis allowed above, and/search?q=duneisn't. - Cache it, but don't use a cached copy for more than about a day; if robots.txt can't be fetched because the server is failing, assume you may not crawl; if it simply doesn't exist (a 4xx), you may crawl.
A real-world trap the chapter's code shows: Python's built-in urllib.robotparser applies rules in file order (first match wins), so it disallows /search/about in this example. A small RFC-style matcher (longest match wins) allows it. Test your parser against the standard, not just its examples.
Politeness beyond robots.txt:
- One connection per host (or a few for very large sites), with a delay between requests, adapted to how fast the site responds: if a page takes 2 seconds, wait longer.
Crawl-delayis a common non-standard directive. It isn't part of RFC 9309; some crawlers honour it and Google doesn't. Honouring it is the polite choice.- Identify yourself with a clear user-agent string and a contact URL.
- Back off on errors (429, 503) and respect
Retry-After(Chapter 25).
Memory code: "Read the sign on the door, knock gently, and leave when asked."