Glossary
Web crawling: discovering pages by following links
Web crawling is the process of finding pages automatically: start from a few URLs, fetch them, collect their links, and keep going while the links stay in scope.
Definition
Search engines index the web by crawling it, but the same idea works at small scale. You may want every product page on a shop, every article in a news archive, or every page of your own site for a broken link check. You rarely have that list up front, so you discover it. A crawl begins with seed URLs and grows by extracting links from each page it downloads.
Crawling and web scraping are often combined: the crawl finds pages, the scraper extracts data from them.
The crawl frontier
The frontier is the set of URLs that have been discovered but not yet fetched. Its design decides almost everything about how a crawl behaves.
- Order. A queue gives breadth first behavior (see breadth first search), a stack gives depth first, and a priority queue lets you fetch important pages first.
- Deduplication. A "seen" set prevents fetching the same URL twice. Normalize URLs before checking: lowercase the host, drop fragments, sort or strip tracking parameters.
- Per host politeness. Group URLs by host and enforce a delay between requests to the same host, even when the frontier holds thousands for it.
- Persistence. Large crawls store the frontier on disk or in a queue service so a restart resumes rather than starting over. See request queue.
Scope rules
Without limits a crawl wanders into the whole internet, or into infinite spaces on one site. Common rules:
- Allowed domains or path prefixes.
- A maximum depth from the seeds.
- Patterns to skip: calendars that generate endless dates, faceted filters with every combination, session ids in URLs, print views.
- A page budget per host.
A small frontier in Python
from collections import deque
from urllib.parse import urljoin, urldefrag, urlparse
def crawl_order(seed, get_links, limit=500):
host = urlparse(seed).netloc
todo, seen = deque([seed]), {seed}
while todo and len(seen) <= limit:
page = todo.popleft()
yield page
for href in get_links(page):
nxt = urldefrag(urljoin(page, href))[0]
if urlparse(nxt).netloc == host and nxt not in seen:
seen.add(nxt)
todo.append(nxt)
Shortcuts that save requests
Before crawling, look for a sitemap. Many sites publish sitemap.xml files listing every important URL with a last modified date, which can replace most of the crawl. Category pages with pagination are the next best source.
Crawls and challenges
Crawls fetch many pages from one host quickly, which is exactly the pattern bot defenses watch for. Keep per host rates modest and expect challenges on large sites anyway. When one appears, park that URL, solve the challenge with a task such as AWS WAF, then retry with the returned cookie. Treat a challenge page as a non result: never extract links from it or count it as crawled.
Common questions
What is a crawl frontier?
The collection of discovered URLs waiting to be fetched, along with the rules for which one comes next.
How do I avoid crawling the same page twice?
Normalize each URL and keep a set of everything already queued or fetched, checking it before adding new links.
What is a crawler trap?
A part of a site that generates endless URLs, such as calendars or filter combinations, which a crawl must exclude by pattern.
Should I crawl or use the sitemap?
Start with the sitemap when one exists; it lists pages directly and saves many requests. Crawl to fill the gaps.
What should a crawler do when it meets a CAPTCHA?
Mark the URL for retry, solve the challenge, and fetch again with the answer, without treating the challenge page as real content.
Keep reading
- CrawlerA crawler is the program that walks a site link by link; politeness rules decide whether it behaves like a good guest.
- Breadth-first crawlingVisiting every link one level at a time versus diving deep down one path, and which crawl order suits which job.
- Request queueThe list of URLs a crawler still has to visit, with retries, priorities and deduplication built into how it is stored.
- Web scrapingWeb scraping means fetching pages and pulling structured data out of them, within the legal and ethical limits of the sites involved.