Sub topic 1
What a Crawler Does and Its Main Components
Prerequisites
This chapter assumes you know basic HTTP (requests, responses, status codes, headers) and basic HTML links. It also assumes you know queues, hash sets, and heaps, and can read short Python programs.
What a crawler does
A web crawler is a program that starts from a list of seed URLs, downloads each page, extracts the links it finds, and repeats on the new links. It stops when it runs out of URLs or reaches a limit, such as a page budget.
The loop is simple. The difficulty is in the details. Many links point to the same page, sites ask crawlers to stay away from some paths, and a crawler that fetches too fast can overload a server. Most of this tutorial covers those details.
The main components
A crawler is a set of small parts that each do one job.
| Component | Job |
|---|---|
| Fetcher | Downloads a URL over HTTP, with a timeout and a size cap |
| Parser | Extracts links from the HTML |
| Normalizer | Turns equivalent URLs into one canonical string |
| Seen set | Remembers which URLs have already been queued |
| Frontier | Holds URLs waiting to be fetched and decides which comes next |
| Robots checker | Tests whether a URL may be fetched |
| Storage | Keeps the downloaded pages and the crawl state |
A first look at the loop
The parser can be small. This one uses the standard library to collect the href of every anchor tag.
from html.parser import HTMLParser
class LinkExtractor(HTMLParser):
def __init__(self):
super().__init__()
self.links = []
def handle_starttag(self, tag, attrs):
if tag == 'a':
for name, value in attrs:
if name == 'href' and value:
self.links.append(value)
parser = LinkExtractor()
parser.feed('<p><a href="/about">About</a> <a href="https://example.org/">Org</a></p>')
print(parser.links) # ['/about', 'https://example.org/']Note that it returns raw href values. /about is relative, so it means nothing until it is resolved against the page URL. That resolution belongs to the normalizer, which is why the parser and normalizer are separate parts.
Why separate parts
Each part can change without touching the others. You can swap an in-memory seen set for a disk-backed one, or replace a single-threaded fetcher with a concurrent one. The frontier is the part that links the rest. It decides the crawl order and enforces politeness, so later chapters return to it often.
Common mistakes
- Fetching a URL as soon as a link is found. Queue it in the frontier instead, so ordering and delays apply.
- Checking the seen set only when a URL is fetched. Check it when a URL is added, or the frontier fills with duplicates.
- Putting parsing, fetching, and queueing in one function. It works for a demo and is hard to extend.
Exercise
Why does the parser return raw href values instead of full URLs, and which component turns them into full URLs?
Show answer
An href such as /about is relative and only has meaning relative to the page it appeared on. Keeping resolution out of the parser keeps each part focused on one job. The normalizer resolves the href against the page URL and maps equivalent forms to one canonical string.
Exercise
Name the components a URL passes through between being discovered on a page and being fetched.
Show answer
The normalizer (canonical form), the seen set (skip duplicates), the frontier (queue and ordering), and the robots checker (permission) come before the fetcher.