How does a crawler avoid fetching the same page twice?
Question
Your crawler finds these links on a page:
HTTPS://Shop.test:443/sale#tophttps://shop.test/salehttps://shop.test/Salehttps://shop.test/item?id=7https://shop.test/item/7, which shows the same page as?id=7
- Which links should it fetch, and how does it decide?
- Over time it will see 1 billion URLs. Keeping them all in a set takes too much memory. What do you use instead, and what can go wrong?
Show hints (3)
- "ONE SPELLING"
Which parts of a URL ignore case? Which part never reaches the server? Turn every link into one standard spelling before you compare.
- "LESS MEMORY"
You don't need the URLs themselves, only an answer to "seen it?". What if a few wrong "yes" answers were acceptable?
- "SAME PAGE TWICE"
Two different URLs can return the same page. What can you compare after the download?
Show checked explanation
Fetch four. The first two links are one URL once you lower-case the scheme and host, then drop the default port and #top. /Sale is a different path. Keep normalized URLs in a seen-set. At 1 billion, use a Bloom filter: 1.25 GB, but about 0.8% of new URLs are wrongly skipped. A content hash then shows that /item/7 repeats ?id=7.
from urllib.parse import urlsplit, urlunsplit DEFAULT_PORT = {"http": 80, "https": 443} def normalize(url): p = urlsplit(url) # the scheme comes back in lower case host = p.netloc.rpartition("@")[2].lower() # drop any user:pass@ host = host.removesuffix(f":{DEFAULT_PORT[p.scheme]}") return urlunsplit((p.scheme, host, p.path or "/", p.query, "")) seen = set() for link in ["HTTPS://Shop.test:443/sale#top", "https://shop.test/sale", "https://shop.test/Sale", "https://shop.test/item?id=7", "https://shop.test/item/7"]: url = normalize(link) # the #fragment was dropped above print("skip " if url in seen else "fetch", url) seen.add(url)
fetch https://shop.test/sale skip https://shop.test/sale fetch https://shop.test/Sale fetch https://shop.test/item?id=7 fetch https://shop.test/item/7
Code run on CPython 3.12, and the Bloom filter numbers compared with a simulated filter. Checked by an editor other than the author.
Walkthrough
Use three checks, cheapest first.
1. Normalize. Write every link in one standard form. Scheme and host ignore case, so lower-case them. Drop :443 for https and :80 for http, and write an empty path as /. Drop the #fragment too: it is never sent to the server, so it can't change the download. The code skips two more standard rules: remove . and .. segments, and write %7E as ~. Now the first two links are both https://shop.test/sale.
2. A seen-set. Check each URL before it joins the queue. If URLs average 80 bytes, 1 billion of them take at least 80 GB. A Bloom filter is much smaller: an array of m bits and k hash functions. To add a URL, set the k bits its hashes point to. To check one, read those k bits. If any is 0, the URL is new for sure. If all are 1, it was probably seen.
After n URLs, a new URL looks seen with a chance of about (1 - e^(-k*n/m))^k. The best k is about 0.7 times the bits per URL. For 1 billion URLs:
| bits per URL | k | memory | new URLs wrongly skipped |
|---|---|---|---|
| 8 | 6 | 1 GB | about 2.2% |
| 10 | 7 | 1.25 GB | about 0.8% |
| 16 | 11 | 2 GB | about 0.05% |
A false "seen" skips that URL every time. The opposite error can't happen: a URL you added never looks new.
3. A content hash. /item?id=7 and /item/7 stay different after normalizing, but they return the same page. After each download, hash the body (with SHA-256, say) and keep the hashes in a set. If the hash is known, don't store the page or follow its links again. This saves the work after the download, not the download.
Common mistakes
- Lower-casing the whole URL. Only the scheme and host ignore case;
/Saleand/salecan be two different pages. - Dropping query parameters by default.
?page=2and?page=3are different pages. Drop a parameter, such as a tracking tag, only when you know it doesn't change the content. - Sizing the Bloom filter once. Built for 1 billion URLs at about 0.8%, it wrongly skips about 14% of new URLs after 2 billion. Plan for growth.
- Treating "seen" as "never fetch again". Pages change. The seen-set only stops duplicate queue entries; a separate schedule decides when to fetch again.
Follow-up questions
- "Two pages differ only by a date or an ad. Does the content hash catch them?" No. One changed byte gives a completely different hash. Use a near-duplicate fingerprint such as SimHash: similar pages get fingerprints that differ in only a few bits. Treat two pages as copies when, say, at most 3 of 64 bits differ.
- "Can the site tell you which URL is the main one?" Often, yes. A page can name its preferred address with
<link rel="canonical" href="...">. Store the page under that address and treat the others as copies. Take it as a hint, since a site can set it wrongly.
What a strong answer covers
Normalizes by the standard rules and keeps the case-sensitive parts. Sizes a Bloom filter with the formula and says what a false positive costs. Adds a content hash for one page at two URLs and knows it misses near-duplicates.
Open in interactive practice deck →