Back to all posts
Save Scraping Bandwidth with ETags and 304 Responses

Save Scraping Bandwidth with ETags and 304 Responses

November 18, 2026

Every time a scraper re-fetches a page that hasn't changed, you pay for it three times: bandwidth on your own connection, bandwidth metered through your proxy, and load on the origin server. For feeds you poll on a schedule — price lists, sitemaps, JSON APIs, changelogs — most of those fetches return bytes you already have. Conditional HTTP requests fix that. Instead of downloading the whole body again, you ask the server "has this changed since I last looked?" and it answers with a tiny 304 Not Modified and no body at all.

This post shows how to add conditional requests to a scraper, why they work well with rotating proxies, and the edge cases that trip people up.

How conditional requests work

Two validators let a server describe a specific version of a resource:

  • ETag — an opaque token like "a1b2c3" that changes whenever the content changes.
  • Last-Modified — an HTTP-date of the last change.

On your first request the server returns the body plus one or both validators. On later requests you send them back as request headers:

  • If-None-Match carries the stored ETag.
  • If-Modified-Since carries the stored Last-Modified.

If the resource is unchanged, the server replies 304 Not Modified with headers only. If it changed, you get a normal 200 OK with the new body and fresh validators. Either way you always hold a correct copy: the one you stored, or the new one.

The bandwidth math is the whole point. A 50 KB JSON feed polled every five minutes is roughly 14 MB/hour if you always download it. With validators, each unchanged poll costs a few hundred bytes.

Why this pairs well with rotating proxies

When you fetch through a rotating proxy, each request may leave from a different IP. That rules out any cache tied to a single connection or IP — but conditional requests rely on neither. The validators and the last known body live on your side, keyed by URL. The server doesn't care which IP asked; it only compares the validator you send against its current version. So you keep the savings on every poll no matter which exit IP the proxy hands you.

Configure the proxy once and reuse it for every request:

import requests

PROXIES = {
    "http": "http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT",
    "https": "http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT",
}

If you fetch through a rotating proxy such as RoProxy, that gateway URL is all you need — the conditional-request logic below behaves identically regardless of which IP a request exits from.

A reusable conditional fetcher

Keep a small cache mapping each URL to its stored validators and body. A plain dict works for a single run; swap in SQLite or Redis when you want it to survive restarts.

def fetch(url, cache):
    headers = {}
    entry = cache.get(url)
    if entry:
        if entry.get("etag"):
            headers["If-None-Match"] = entry["etag"]
        if entry.get("last_modified"):
            headers["If-Modified-Since"] = entry["last_modified"]

    resp = requests.get(url, headers=headers, proxies=PROXIES, timeout=30)

    if resp.status_code == 304:
        return cache[url]["body"], True          # unchanged, reuse stored body

    resp.raise_for_status()
    if resp.status_code == 200:
        cache[url] = {
            "etag": resp.headers.get("ETag"),
            "last_modified": resp.headers.get("Last-Modified"),
            "body": resp.text,
        }
        return resp.text, False                   # fresh content

The second return value tells your pipeline whether the content changed, so you can skip downstream parsing when it didn't. That is often a bigger win than the bandwidth itself: no need to re-parse, re-diff, or re-store identical data.

Keep request headers stable

An ETag is only valid for the exact request that produced it. If your first fetch sends Accept-Encoding: gzip and a later one doesn't, or you rotate User-Agent values between calls, the server may treat them as different representations and ignore your validator. Pick one set of headers per URL and reuse it so the validators keep matching.

Verifying it with cURL

You can confirm a server supports conditional requests before writing any code. First fetch and capture the validators:

curl -s -D headers.txt \
  -x http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT \
  https://example.com/data.json -o body.json
grep -i '^etag:' headers.txt

Then replay the validator and check the status code:

ETAG=$(grep -i '^etag:' headers.txt | tr -d '\r' | awk '{print $2}')
curl -s -o /dev/null -w "%{http_code}\n" \
  -H "If-None-Match: $ETAG" \
  -x http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT \
  https://example.com/data.json

A 304 in the output means conditional requests work. To confirm which IP the request exited from, ask an echo service through the same proxy:

curl -s -x http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT \
  https://api.ipify.org?format=json

Edge cases that bite

  • Servers that ignore validators. Some origins, and some intermediaries, always return 200 with a full body. Log how often you actually get 304; if it is never, the savings will not materialize and you should lean on your own change detection instead.
  • Weak ETags. A value like W/"a1b2c3" is a weak validator — semantically equivalent content, byte-level differences ignored. Echo it back verbatim in If-None-Match; do not strip the W/.
  • Vary headers. If the response says Vary: Accept-Language, the ETag is per-language. Keep the relevant request headers constant, or key your cache by URL plus the varying header, so you never serve the wrong version.
  • Only cache safe requests. Conditional requests are meaningful for GET and HEAD. Do not attach stored validators to POST or other state-changing calls.
  • Etiquette still applies. A 304 reduces load, but keep honoring robots.txt, your rate limits, and the site's terms. Cheaper polling is not a license to poll aggressively.

Measuring the payoff

Log two counters per URL: total requests and 304 responses. Their ratio is your hit rate. A well-behaved feed you poll every few minutes should sit well above 50% once the first fetch has primed the cache. Track bytes downloaded before and after — you and your proxy provider both benefit when most polls shrink to a few hundred bytes. If a URL's hit rate stays near zero, the server is not honoring validators; move it to explicit change detection rather than keep paying for full downloads.

Where to store the cache

For a one-off script, an in-memory dict is fine. For scheduled jobs that run in separate processes, persist the cache so you don't lose validators between runs:

  • SQLite: one row per URL with etag, last_modified, and a content hash.
  • Redis: SET url:<hash> <json> with no expiry, so validators live as long as the data does.

Store a hash of the body alongside it. If a 200 returns a body whose hash matches the old one, treat it as unchanged even when the server skipped the 304 — a cheap safety net against wasteful reprocessing.

Conclusion

Conditional requests are one of the least glamorous and most effective optimizations in a scraper's toolkit. Send the validators you already have, let the server answer 304 when nothing changed, and you cut bandwidth, proxy traffic, and origin load in one move. Wire the fetcher above into your poller, keep routing fetches through RoProxy, and log your 304 rate to confirm the payoff — unchanged pages will cost you almost nothing.