Trafilatura’s download module handles all HTTP communication, including SSL negotiation, retries, proxy support, and decoding. It automatically selects betweenDocumentation 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.
urllib3 (default) and pycurl (when installed) for optimal performance. The three public functions — fetch_url(), fetch_response(), and load_html() — cover progressively more detailed levels of access, from a plain decoded string all the way to a parsed lxml tree.
fetch_url()
Downloads a web page and returns the decoded HTML content as a Unicode string. This is the simplest and most commonly used download function, suitable for use with extract() and most other Trafilatura functions.
The fully-qualified URL of the page to download (must begin with
http:// or https://).Disable SSL certificate verification. Useful when a host has a misconfigured or self-signed certificate. Trafilatura will automatically retry with
no_ssl=True after an SSLError, so manually setting this is rarely necessary.A
configparser.ConfigParser instance controlling download behaviour (timeouts, max file size, max redirects, user agents, cookies). Superseded by options when both are provided.A pre-built
trafilatura.settings.Extractor object. When provided, options.config is used as the configuration and overrides the config parameter.str | None — the decoded HTML content as a Unicode string, or None if the download fails, returns a non-200 status, or the response is outside the acceptable size limits.
Examples
fetch_response()
Downloads a web page and returns a full Response object containing the raw bytes, HTTP status, final URL (after any redirects), and optionally the decoded HTML and response headers. Use this when you need lower-level control — for example, to inspect redirect chains, access response headers, or handle the binary data yourself.
The URL to fetch.
Decode the raw bytes and populate
response.html with a Unicode string. When False, response.html is None and only response.data (bytes) is populated.Disable SSL certificate verification. Trafilatura retries automatically on
SSLError, so this is typically not needed.Store the HTTP response headers in
response.headers as a lowercase-keyed dictionary.Configuration object controlling download parameters.
Response | None — a Response object, or None if the download fails.
The Response object
The Response class stores all information gathered during a single HTTP request:
| Attribute | Type | Description |
|---|---|---|
data | bytes | Raw response body bytes |
html | str | None | Decoded HTML string (populated when decode=True) |
status | int | HTTP status code (e.g. 200, 404) |
url | str | Final URL after following all redirects |
headers | dict[str, str] | None | Lowercase-keyed response headers (populated when with_headers=True) |
Response also supports bool(response) (truthy when data is not None), str(response) (renders as HTML if decoded, otherwise decodes bytes), and response.as_dict() which returns all attributes as a plain dictionary.
Examples
load_html()
Parses an HTML document into an lxml element tree. Accepts a Unicode string, raw bytes, a pre-parsed HtmlElement (returned as-is), or a Response-like object with a .data attribute. Handles encoding detection, faulty HTML repair, and falling back to byte-level parsing when needed.
The HTML input to parse. Accepted types:
str— a Unicode HTML stringbytes— raw byte content; encoding is auto-detectedlxml.html.HtmlElement— returned unchanged without re-parsing- Any object with a
.dataattribute (e.g., aResponseorurllib3response) —.datais extracted and parsed as bytes
lxml.html.HtmlElement | None — the root element of the parsed HTML tree, or None if parsing fails or the input is not valid HTML.
Examples