Sub topic 5
Avoiding Repeat Visits with a Seen Set
A crawler follows links, and the web is full of cycles. Page A links to B, and B links back to A. Without a record of visited URLs, the crawler loops forever and hits the same servers over and over. This chapter covers the seen set: the data structure that prevents this.
This chapter assumes you know Python sets and hashing, and that you have read the chapter on normalizing URLs. The seen set only works if equivalent links have already been mapped to one string.
The core idea
A seen set is a hash set of normalized URLs. Before a URL enters the frontier, the crawler checks the set. If the URL is present, it is dropped. If not, it is added to the set and queued.
The Frontier.add method from the frontier chapter already does this:
def add(self, url):
if url in self.seen:
return
self.seen.add(url)
...Mark at enqueue time, not at fetch time
A common error is to add a URL to the seen set only after fetching it. Between queueing and fetching, many pages may link to the same URL. Each one would queue another copy. The frontier then grows with duplicates, and the page is fetched several times.
Marking at enqueue time means "seen" really means "known to the crawler". A URL in the set may still be waiting in a queue. If you need to know whether a page was actually downloaded, keep that as a separate record.
What belongs in the key
The key is the normalized URL string. Two details matter:
- The fragment is dropped during normalization, so
/page#topand/pageshare a key. - The query string is kept.
/item?id=1and/item?id=2are different pages. Sorting parameters or removing tracking parameters is a policy choice, and a wrong choice either merges distinct pages or leaves duplicates.
Memory cost
A set of Python strings costs roughly 100 bytes or more per URL once you count the string object and the hash table slot. At 100 million URLs, that is around 10 GB. Two common reductions:
| Approach | Memory per URL | Trade-off |
|---|---|---|
| Store full strings | 100+ bytes | Exact, simple |
| Store a 64-bit hash of the URL | About 8 bytes of data, plus table overhead | Tiny chance two URLs collide and one is skipped |
| Bloom filter | A few bits | Never forgets a seen URL, but sometimes reports a new URL as seen |
A Bloom filter's false positives cause a small fraction of pages to be silently skipped. It never causes repeat visits. Many crawlers accept that loss in exchange for a fixed, small memory footprint.
Hashing example
import hashlib
def url_key(url):
digest = hashlib.blake2b(url.encode('utf-8'), digest_size=8).digest()
return int.from_bytes(digest, 'big')
seen = set()
def is_new(url):
key = url_key(url)
if key in seen:
return False
seen.add(key)
return True
print(is_new('https://example.com/a')) # True
print(is_new('https://example.com/a')) # FalseWith 64-bit keys, collisions become likely only at billions of URLs. The birthday bound is about 4 billion keys for a 50% chance of at least one collision.
Exercise
A crawler adds a URL to its seen set only after the page is fetched. Page P links to U, and 50 other queued pages also link to U. What goes wrong, and what is the fix?
Show answer
Each of those pages is processed before U is fetched, so U is not yet in the seen set and gets queued up to 51 times. U is then fetched repeatedly. The fix is to check and add to the seen set when the URL is enqueued.