A focused crawler — also known as a web spider — starts from one or more seed URLs, downloads each page, extracts the internal hyperlinks it finds, and adds those links to a queue for further exploration. Trafilatura’s crawler is designed for intra-domain use: it stays within the target website, prioritising navigation pages (archives, category listings, pagination) to maximise link discovery in as few requests as possible.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/adbar/trafilatura/llms.txt
Use this file to discover all available pages before exploring further.
Intra-Domain vs. Inter-Domain Crawling
Intra-domain (focused)
Crawls within a single website. Manageable in scope, easy to tune, and the recommended starting point for building a text corpus from a known source.
Inter-domain (broad)
Hops across multiple websites. Requires much more careful deduplication, politeness logic, and RAM for URL tracking. Better served by dedicated crawl infrastructure.
How the Crawler Works
Crawling starts with a seed URL. Trafilatura fetches the page, extracts internal links, and classifies them as either navigation pages (e.g./category/news/, /page/2/) or content pages (individual articles). Navigation pages are prioritised in the queue so that the crawler reaches as many content pages as possible with the fewest iterations.
Visited URLs are marked in an internal UrlStore to ensure each page is fetched at most once. Crawl-delay directives from robots.txt are honoured automatically.
Focused Crawler in Python
Basic Usage
Importfocused_crawler from trafilatura.spider and call it with the homepage URL:
to_visit— list of URLs queued for future visits (the crawl frontier)known_links— list of all URLs discovered so far (visited and queued)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
homepage | str | — | Starting URL for the crawl |
max_seen_urls | int | 10 | Stop after visiting this many pages |
max_known_urls | int | 100000 | Stop if the total known URL count exceeds this |
todo | list[str] | None | Previously generated crawl frontier to resume from |
known_links | list[str] | None | Previously known URLs (prevents revisiting) |
lang | str | None | Two-letter ISO 639-1 code — bias link selection toward this language |
config | ConfigParser | default config | Custom Trafilatura configuration |
rules | RobotFileParser | auto-fetched | Pre-loaded robots.txt rules |
prune_xpath | str | None | XPath expression to remove unwanted elements from pages before link extraction |
Step-by-Step Iteration
Because the crawler returns its state after each run, you can save progress between sessions and resume later. Pass the returnedto_visit and known_links back into the next call:
Checking Navigation Progress
Useis_still_navigation() to test whether there are still navigation pages (archive pages, category indexes, etc.) in the queue. When it returns False, the crawler has likely exhausted the site’s link structure and remaining URLs are mostly content pages:
Full Iteration Pattern
The following pattern runs the crawler in a loop until the queue is empty or a size limit is reached:CLI Crawling
Trafilatura exposes three discovery modes on the command line:- --crawl
- --explore
- --probe
Crawl a website starting from the given URL, stopping at a maximum of 30 page visits or when all pages have been seen:
The
--list flag does not apply to --crawl or --explore. Instead, URLs are returned as a plain list so you can inspect and filter them before committing to a bulk download run.Parallel Crawling
Pass a file of seed URLs with-i/--input-file to crawl multiple websites concurrently:
Politeness Rules
Trafilatura’s crawler enforces politeness automatically:robots.txtcompliance — the crawler fetches and parsesrobots.txtfor each domain it visits, respectingDisallowdirectives and honoringCrawl-Delayvalues- Automatic sleep — if the website’s
robots.txtspecifies aCrawl-Delay, that value is used; otherwise theSLEEP_TIMEsetting in your configuration file applies (default: 5 seconds)
focused_crawler() to avoid re-fetching them on every call:
Deduplication
The crawler performs deduplication at two levels:- URL-level — the internal
UrlStoretracks every URL that has been seen, preventing the same page from being fetched twice across multiple iterations - Content-level — when text extraction is combined with crawling, near-duplicate pages can be filtered out using Trafilatura’s deduplication module
Performance Considerations
The main resource constraints for large crawls are:Time
Crawl delay between requests to the same host is the primary speed bottleneck. Crawling many different domains in parallel amortises this wait.
Bandwidth
Concurrent downloads across many domains can saturate a connection. Use
threads and sleep_time to tune throughput.RAM
Each known URL occupies memory. Above millions of URLs, RAM becomes the limiting factor. Keep
max_known_urls within a reasonable bound or use external storage for very large crawls.Adjusting Crawl Settings
TheSLEEP_TIME default and other download parameters can be changed in a settings.cfg file:
focused_crawler():