Skip to main content

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.

Trafilatura simplifies the process of turning raw HTML into structured, meaningful data. This guide walks you through the core workflow — from installation to your first extraction — using both the Python API and the command-line interface.
1

Install Trafilatura

Install Trafilatura from PyPI using pip:
pip install trafilatura
Verify the installation:
trafilatura --version
For full installation options, optional extras, and troubleshooting, see the Installation guide.
2

Fetch and extract text with Python

The two core functions are fetch_url() to download a page and extract() to pull out the main content. The only required argument to extract() is the downloaded HTML — everything else is optional.
from trafilatura import fetch_url, extract

# Download the HTML of a web page
downloaded = fetch_url("https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/")

# Extract the main text (plain text by default)
result = extract(downloaded)
print(result)
extract() strips boilerplate — navigation menus, ads, headers, footers — and returns only the article body and, by default, any reader comments.
3

Customise the output format

Trafilatura supports several output formats and lets you include or exclude specific content elements:
from trafilatura import fetch_url, extract

downloaded = fetch_url("https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/")

# Preserve document structure as XML
result_xml = extract(downloaded, output_format="xml")

# JSON output with metadata, without comments
result_json = extract(
    downloaded,
    output_format="json",
    with_metadata=True,
    include_comments=False,
)

# Markdown with metadata
result_md = extract(downloaded, output_format="markdown", with_metadata=True)

print(result_json)
Available output formats are txt (default), markdown, csv, json, html, xml, and xmltei.
4

Run from the command line

Trafilatura ships with a trafilatura CLI command. Pass a URL with the -u / --URL flag:
# Extract main text from a URL (plain text output)
trafilatura -u "https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/"
You can also pipe an existing HTML file directly:
# Pipe a local HTML file
cat myfile.html | trafilatura

# Equivalent shorthand
< myfile.html trafilatura
CLI flags mirror the Python API options and can be combined freely:
# Output JSON and skip tables
< myfile.html trafilatura --json --no-tables
Display the full list of available options:
trafilatura -h

Fast Mode

By default, Trafilatura uses multiple extraction algorithms as fallbacks to maximise recall. If speed is a priority and you are comfortable with a potential minor drop in coverage, enable fast mode to skip the fallback algorithms:
from trafilatura import fetch_url, extract

downloaded = fetch_url("https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/")

# Faster extraction without fallback algorithms
result = extract(downloaded, fast=True)
print(result)

Extract All Text with html2txt

If you need all visible text on a page — rather than just the main content block — use html2txt. This is similar to tools like html2text but operates on an already-downloaded HTML string:
from trafilatura import fetch_url, html2txt

downloaded = fetch_url("https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/")

# Returns all visible text, including navigation and footers
all_text = html2txt(downloaded)
print(all_text)

Extract Metadata Only

Use extract_metadata() to retrieve structured metadata (title, author, date, site name, etc.) without the full body text:
from trafilatura import fetch_url, extract_metadata

downloaded = fetch_url("https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/")

metadata = extract_metadata(downloaded)
print(metadata)

Bare Extraction (Low-Level)

bare_extraction() returns a Python dictionary directly, without serialisation to a string format. This is the most efficient option when you are processing results programmatically:
from trafilatura import fetch_url, bare_extraction

downloaded = fetch_url("https://github.blog/2019-03-29-leader-spotlight-erin-spiceland/")

# Returns a dict with text, metadata, and structural information
data = bare_extraction(downloaded)
print(data["title"])
print(data["text"])
Explore Trafilatura’s full feature set interactively in the official Trafilatura Overview Python Notebook. For a complete list of extract() parameters and advanced usage, see the Usage documentation and Tutorials.

Build docs developers (and LLMs) love