Skip to content

Lesson 4 of 9 · 25m

HTTP Scrapers That Degrade Gracefully

Implement bounded, polite HTTP collectors with explicit identity, caching, timeouts, retries, backoff, and failure classification.

The simplest scraper is one process fetching one page. The first production scraper you operate is a distributed system wearing a trench coat: queues, retries, caches, partial failures, DNS weirdness, TLS errors, stale parsers, and remote servers having a bad day. If you do not design degradation, scale will design it for you.

Be identifiable and polite

Set a real user agent and honor rate limits. A good user agent tells the operator who you are and how to contact you:

import httpx

client = httpx.Client(
    headers={"User-Agent": "SudoResearchBot/1.0 (+https://sudosecurity.org/contact)"},
    timeout=httpx.Timeout(10.0, connect=5.0),
    follow_redirects=True,
    max_redirects=5,
)

Do not impersonate a consumer browser when simple HTTP is enough. Misrepresentation removes the operator's ability to make an informed choice about your traffic.

Retries need a taxonomy

Not every failure deserves another attempt:

timeout/reset/5xx          transient, retry with capped backoff
429                        rate limited, slow down and honor Retry-After
404/410                    entity absent, record and do not hammer
403/DNS/TLS failure        access or infrastructure decision, alert or review

Use exponential backoff with jitter and a hard cap:

for attempt in range(3):
    try:
        response = client.get(url)
        break
    except httpx.TransportError:
        sleep(min(2 ** attempt + random.random(), 8))
else:
    raise

Retrying a 403 thirty times is not resilience. It is escalation.

Respect server hints and conditional fetches

Retry-After, cache headers, and rate-limit headers are operational signals. Choose conservative defaults even when no formal limit is published; one request per second per host is a starting point, not an entitlement. Use validators when available:

r = client.get(url, headers={"If-None-Match": etag} if etag else {})
if r.status_code == 304:
    mark_unchanged(url)

For pages without validators, content hashing still helps downstream dedupe.

Separate fetch failure from parse failure

A 200 response with a changed layout is not a transport success in business terms. Record both dimensions:

fetch_status   ok / timeout / http_error / blocked / invalid
parse_status   ok / empty / schema_drift / partial
http_status    exact numeric response, with content hash and concise error class

That separation prevents operators from re-crawling thousands of perfectly valid pages because a selector moved.

Field notes

  • Set connect and read timeouts separately; infinite hangs are queue poison.
  • Retry counts multiply request volume. A 1,000-page crawl with 3 retries is potentially 4,000 requests.
  • HTTP 200 does not mean useful content; many anti-bot and error pages return 200.
  • Do not distribute load across many IPs to make a host-level limit harder to enforce.
  • Record redirect chains. Sudden redirects to homepages often mean expired entities or geolocation logic.

Was this lesson helpful?

Sign in to rate