Sub topic 14
Recrawling and Keeping Content Fresh
A crawl that runs once produces a snapshot that goes stale. This chapter assumes you know the frontier, content hashing (from the duplicate-detection chapter) and basic HTTP headers. It covers how a crawler decides when to revisit a page and how to make each revisit cheap.
Why a recrawl policy is needed
Pages change at different rates. A news homepage changes every few minutes, and an old documentation page may not change for years. Revisiting everything at one fixed interval wastes bandwidth on static pages and leaves fast-changing pages out of date. Every revisit also costs the target site a request, so the politeness limits from earlier chapters still apply.
A recrawl policy answers two questions:
- Which URLs are due for a revisit?
- How can a revisit avoid downloading unchanged content?
Cheap revisits with conditional requests
When a server sends an ETag or Last-Modified header, store it with the URL. On the next visit, send it back as If-None-Match or If-Modified-Since. If the page is unchanged, the server answers 304 Not Modified with no body.
import urllib.error
import urllib.request
def conditional_fetch(url, etag=None, last_modified=None, timeout=10):
headers = {'User-Agent': 'MyCrawler'}
if etag:
headers['If-None-Match'] = etag
if last_modified:
headers['If-Modified-Since'] = last_modified
req = urllib.request.Request(url, headers=headers)
try:
with urllib.request.urlopen(req, timeout=timeout) as resp:
body = resp.read(2_000_000)
return resp.status, body, resp.headers.get('ETag'), resp.headers.get('Last-Modified')
except urllib.error.HTTPError as err:
if err.code == 304:
return 304, None, etag, last_modified
raiseurllib raises HTTPError for a 304, so the code catches it and treats it as a normal result.
Adapting the interval
Instead of a fixed schedule, give each URL its own revisit interval. Shorten it when the page changed and lengthen it when it did not.
def next_interval(interval, changed, minimum=3600, maximum=30 * 86400):
interval = interval / 2 if changed else interval * 2
return max(minimum, min(maximum, interval))Intervals are in seconds, here between one hour and thirty days. Store due = now + interval for each URL and keep the due times in a heap. The scheduler pops URLs whose due time has passed and hands them to the frontier. The per-host queues still enforce the delays, so recrawls never bypass politeness.
Prioritizing under a budget
A crawler has limited fetches per day. When more URLs are due than it can fetch, it needs a priority rule. Common signals are:
- Change history: pages that changed on most visits go first.
- Importance: pages with many inbound links or high traffic go first.
- Age: the longer a page has been overdue, the higher its priority.
Also recrawl the pages that link to new content, such as index and feed pages. Those are the pages where new URLs first appear.
Common errors
- Resetting the interval to a small value after a transient failure. A timeout says nothing about whether the page changed, so leave the interval alone and retry later.
- Counting tiny changes, such as a timestamp or ad block, as real changes. Hash the extracted main text instead of the raw HTML.
- Forgetting deleted pages. After repeated 404 or 410 responses, lengthen the interval sharply or drop the URL.
Exercise
A page returns 200 on every recrawl, but its body hash never changes. Using the policy above, what happens to its interval, and what would you check on the server's behavior?
Show answer
The interval doubles on each visit until it reaches the maximum, because the body is unchanged. You should check whether the server ignores If-None-Match and If-Modified-Since or sends validators that change on every request. If so, the crawler downloads the full body each time even though the content is the same.