Sub topic 2
Fetching a Page: Requests, Status Codes, and Redirects
This chapter assumes you know what an HTTP request and response are, can read basic HTML, and can follow short Python snippets. Fetching is the first step a crawler takes on a URL. Everything later, such as link extraction and indexing, depends on what the fetch returns.
What a crawler sends and receives
A crawler sends an HTTP GET request and reads the response. The response has a status code, headers, and usually a body. The crawler identifies itself with the User-Agent header, which is also the name that robots.txt rules match against.
Headers that matter to a crawler:
Content-Typetells the crawler whether the body is HTML, a PDF, an image, or something else.Locationgives the target of a redirect.ETagandLast-Modifiedlet the crawler ask later whether the page changed.Retry-Aftertells the crawler how long to wait after a busy or rate-limit response.
Status codes and what the crawler does
The status code decides what happens to the URL next.
| Status | Meaning | Typical crawler action |
|---|---|---|
| 200 | OK | Parse the body and extract links. |
| 301, 308 | Permanent redirect | Follow it and treat the target as the preferred URL. |
| 302, 307 | Temporary redirect | Follow it, but keep the original URL as the preferred one. |
| 304 | Not modified | Reuse the stored copy. |
| 404, 410 | Not found, gone | Drop the URL from the index after repeated checks. |
| 429, 503 | Too many requests, unavailable | Slow down and retry later. |
A 404 or 410 means the page is missing, so the crawler stops treating it as content. A 5xx error is usually treated as temporary, but a URL that keeps failing is crawled less often.
Following redirects
A redirect response has a Location header. The crawler requests that new URL and repeats until it gets a final response. Crawlers limit the number of hops, because chains and loops waste time. A page that redirects to itself would otherwise never finish.
A fetch in Python
This script requests a page without following redirects automatically, so you can see each hop. It sets a user agent and a timeout.
import http.client
from urllib.parse import urljoin, urlparse
def fetch(url, max_hops=5):
for hop in range(max_hops + 1):
parts = urlparse(url)
conn_class = (http.client.HTTPSConnection
if parts.scheme == "https"
else http.client.HTTPConnection)
conn = conn_class(parts.netloc, timeout=10)
path = parts.path or "/"
if parts.query:
path += "?" + parts.query
conn.request("GET", path, headers={"User-Agent": "ExampleBot/1.0"})
resp = conn.getresponse()
print(hop, resp.status, url)
if resp.status in (301, 302, 303, 307, 308):
location = resp.getheader("Location")
conn.close()
if not location:
return None
url = urljoin(url, location)
continue
body = resp.read()
conn.close()
return resp.status, body
return None
result = fetch("http://example.com/")
if result:
print(result[0], len(result[1]), "bytes")urljoin matters here because Location can be a relative path such as /new-page.
Common errors
- Treating a soft 404 as a success. Some sites return 200 with a "not found" message. The crawler sees a valid page, so the empty page may be indexed.
- Using a temporary redirect for a permanent move. Search engines may keep the old URL as the preferred one.
- Redirect chains and loops. Each hop costs a request, and loops end in failure.
- Blocking by status. Returning 403 or 429 to crawlers makes pages unreachable, even if they are fine for browsers.
Exercise
A site moves /old-guide to /guide permanently. Which status code should /old-guide return, and which header tells the crawler the new address?
Show answer
It should return 301 (or 308). The Location header carries the new address, /guide. A permanent code signals that the new URL should replace the old one.
Exercise
A crawler gets a 200 response for /missing-page, but the body says "Page not found". What is this called, and why is it a problem?
Show answer
It is a soft 404. The crawler sees a successful response, so it may index an empty or useless page instead of dropping the URL. The server should return 404 or 410.