Sub topic 14
Recrawling and keeping the index fresh
This chapter assumes you know HTTP status codes and headers, have read the chapters on fetching and on crawl budget, and can read short Python snippets. A crawl is not a one-time event. Pages change, move, and disappear, so the crawler must revisit them. The question is how often to revisit each one.
Why recrawling is a scheduling problem
Fetching every known URL constantly would waste crawl budget and load servers. Fetching too rarely leaves stale results. A scheduler balances the two by giving each URL a revisit interval.
The interval usually comes from signals such as:
- How often the page changed on past visits.
- How important the page seems, for example how many links point to it.
- Hints from the site, such as
lastmodin a sitemap. - The type of page. A news homepage changes hourly. An archived article rarely changes.
A page that changed on each of its last five visits gets a shorter interval. A page that never changed gets a longer one.
Conditional requests save work
A crawler can ask the server whether a page changed without downloading it again. On the first fetch, the server may send an ETag or a Last-Modified header. On the next visit, the crawler sends them back as If-None-Match or If-Modified-Since. If nothing changed, the server replies 304 Not Modified with no body.
from urllib.request import Request, urlopen
from urllib.error import HTTPError
def recheck(url, etag=None, last_modified=None):
req = Request(url)
if etag:
req.add_header("If-None-Match", etag)
if last_modified:
req.add_header("If-Modified-Since", last_modified)
try:
with urlopen(req, timeout=10) as resp:
return "changed", resp.headers.get("ETag"), resp.headers.get("Last-Modified")
except HTTPError as err:
if err.code == 304:
return "unchanged", etag, last_modified
raise
print(recheck("https://example.com/"))This only helps if the server implements these headers correctly. Some servers always return 200 or send a new ETag on every request. The crawler then has to download and compare content itself.
How status codes affect the index
The result of a recrawl changes what the index holds.
| Response | Typical effect |
|---|---|
| 200 with changed content | Page is reindexed with the new text. |
| 304 | Stored copy stays. Only the schedule changes. |
| 301 or 308 | The new URL is crawled. Over time the old one is replaced. |
| 404 or 410 | The page is dropped after one or more confirming visits. |
| 5xx or timeout | The old copy is usually kept for a while. The crawler retries later and may slow down. |
Engines often wait for repeated failures before removing a page, because a short outage should not erase it. A 410 Gone is a stronger removal signal than 404.
Sitemap `lastmod` and its limits
A sitemap's lastmod tells the crawler when to look again. Crawlers learn whether a site's dates can be trusted. If lastmod changes on every build even though the content did not, the crawler learns to ignore it. Update lastmod only for real content changes.
Common errors
- Expecting an edit to show in results immediately. There is a delay between a recrawl, reindexing, and the change reaching serving.
- Returning
200with an error message on missing pages. The crawler sees a valid page and keeps it. Return a real404or410. - Blocking a changed page in
robots.txt. The crawler cannot fetch it, so the old copy remains.
Exercise
A site rebuilds nightly and sets every sitemap lastmod to the build time, though most pages never change. What happens over time, and what should the site do instead?
Show answer
The crawler finds that lastmod often does not match real changes, so it trusts the field less and may ignore it. The site should set lastmod only when a page's content actually changes. It should also send correct ETag or Last-Modified headers so conditional requests work.