Sub topic 3
Fetching Many Pages Concurrently
Assume you know basic HTTP, HTML links, Python, queues, sets, and heaps. A crawler can fetch several pages at once, but it must still enforce a delay between requests to the same host.
Concurrency Without Losing Politeness
Concurrency means making progress on multiple network requests while one request waits for a response. Use a worker pool for different hosts, and let the URL frontier decide when each host is ready.
from concurrent.futures import ThreadPoolExecutor
from urllib.request import urlopen, Request
def fetch(url):
request = Request(url, headers={"User-Agent": "ExampleCrawler/1.0"})
with urlopen(request, timeout=10) as response:
if response.headers.get_content_type() != "text/html":
return url, None
return url, response.read(1_000_000)
urls = ["https://example.com", "https://www.python.org"]
with ThreadPoolExecutor(max_workers=4) as pool:
for url, body in pool.map(fetch, urls):
print(url, len(body) if body else 0)A worker is a thread or process that performs one unit of work. Network-bound crawlers often use threads or asynchronous tasks. Limit workers, set timeouts, and retry only temporary failures.
Limits and Duplicate Content
A crawl trap is a link pattern that creates nearly unlimited URLs, such as calendar pages or changing query parameters. Set a maximum depth, page budget, and URL limit. A depth records how many links separate a page from a seed.
Content deduplication catches different URLs that return the same page. Hash the response body for exact matches. Near-duplicate detection can compare normalized text or similarity fingerprints.
Storage and Recrawling
Store the URL, fetch time, status, headers, content hash, and body location. Checkpoint the frontier and seen set regularly so a crash does not restart the crawl.
Recrawling means visiting known URLs again. Give each URL a next-fetch time based on its importance and observed change rate. Use conditional requests with ETag or Last-Modified when supported.
| Choice | Main trade-off |
|---|---|
| Raw pages | Preserves evidence but uses more storage |
| Extracted text | Saves space but loses page details |
JavaScript and Distributed Crawls
Some pages create links only after JavaScript runs. A browser renderer can execute that code, but it costs more CPU, memory, and time. Use it selectively after ordinary HTML fetching finds insufficient content.
A distributed crawler partitions URLs across machines, commonly by host hash. Each machine owns particular hosts, which helps preserve per-host ordering. Shared storage or durable queues prevent lost work.
Monitoring
Track fetch rate, queue size, latency, status codes, timeout rate, bytes downloaded, duplicate rate, and per-host delay violations. Alerts should identify stalled queues, repeated failures, and unexpected growth in URL count.
Exercise
Why can four workers still be polite?
Show answer
The frontier can assign work concurrently while enforcing a separate next-request time for each host.