Trafilatura Drops 75% of Table Text
A RAG index built with one extraction call loses its tables and code blocks, and nothing in the output flags it until retrieval misses numbers.
📜 Table of Contents
📖 Keep reading
Trafilatura kept a median of 25% of the table text on the 27 article pages tested today. On the worst page it kept 11.7%. Plain BeautifulSoup get_text kept 97.6%. If a RAG pipeline turns blog posts and docs into chunks with one extraction call, tables are the part that disappears first, and nothing in the output says so.
The token savings everyone quotes for readability extraction are real and measurable: 31.6% fewer input tokens across the same 27 pages. The question is what got dropped to get there.
What was measured
- 33 article links pulled from 11 RSS feeds on 2026-11-27 (Simon Willison, Julia Evans, Martin Fowler, GitHub Blog, Ars Technica, The Register, TechCrunch, The Verge, Cloudflare Blog, Hugging Face Blog, Stack Overflow Blog).
- 27 returned HTTP 200 to a plain Python client. Three Ars Technica pages answered 405, one feed returned no entries.
- Four extractors on identical HTML: BeautifulSoup get_text, readability-lxml summary, trafilatura 2.4.0 default with tables and comments, trafilatura with favor_precision.
- Token counts with tiktoken o200k_base. Latency on a 2 vCPU / 2 GB VPS, single run per page.
- Code and table retention: the text inside every pre and table element of the raw HTML as ground truth, then 5-gram coverage against each extractor output.
The headline numbers
| Extractor | Total tokens, 27 pages | Median per page | Median latency |
|---|---|---|---|
| BeautifulSoup get_text | 94,692 | 2,856 | 24 ms |
| readability-lxml | 60,998 | 1,742 | 39 ms |
| trafilatura default | 64,760 | 1,867 | 27 ms |
| trafilatura favor_precision | 63,117 | 1,803 | 26 ms |
Median reduction against the naive baseline: 29.7% for trafilatura, 33.3% for readability. The extremes matter more. The Verge FCC article went from 1,647 tokens to 341 under trafilatura, a 79.3% cut. Cloudflare post on agent search went from 4,431 to 1,975. GitHub post about developer gifts went from 2,918 to 897.
Some of that is menu, footer, cookie banner and related-links furniture. Some of it is not.
Tables: three quarters of the text is gone
Five pages had tables with at least 40 characters of text. Comparing what each extractor kept:
| Page | Table chars in HTML | BeautifulSoup | readability | trafilatura |
|---|---|---|---|---|
| huggingface.co/blog/training-swarms | 733 | 92.8% | 92.8% | 11.7% |
| huggingface.co/blog/nvidia/nemotron-parse | 357 | 100% | 100% | 19.5% |
| huggingface.co/blog/ggml-org/llamacpp-amd-bench | 174 | 100% | 100% | 25.0% |
| github.blog application modernization | 1,231 | 95.7% | 95.7% | 50.5% |
| blog.cloudflare.com/ruby2shell-waf-protection | 1,369 | 97.6% | 97.6% | 66.1% |
Median retention on those five pages: 97.6% for BeautifulSoup, 97.6% for readability, 25.0% for trafilatura. Trafilatura table handling is configurable and does better on simple two-column layouts, but on these five it flattened rows into a running string or dropped rows that repeat identical cells. A benchmark table where every row shares the same model name is exactly the shape it loses.
This is the failure that hurts a RAG index quietly: the chunk that should read llama.cpp on AMD, 1,024 tokens, 41 tokens per second arrives as prose fragments, the embedding points at nothing useful, and recall drops on the only queries that needed numbers.
Code blocks mostly survive, with two exceptions
Nine pages contained pre blocks worth measuring. BeautifulSoup and readability kept a median 93.5% of the code text. Trafilatura kept 91.7%, and two pages fell off a cliff: the Hugging Face training-swarms post kept 39% of its code, and Cloudflare search-for-agents post kept 82.8%. In both cases the lost lines were indented snippets that trafilatura re-flowed into paragraph text, so the Python semantics disappeared while the words stayed.
If the corpus is tutorial content where code is the answer, that is the same class of bug as the table loss, just less frequent. Extract code separately, or post-check it.
Readability fails in the opposite direction
Readability removes the most boilerplate, and it also removes content it cannot classify. Two examples from the run: GitHub post about developer gifts, 3,386 tokens naive against 1,137 after readability; The Verge FCC article, 1,647 against 310. A reader would call those truncations, not cleaning. Readability is tuned for pages with an article body and a clear main element; landing pages and category templates lose whole sections.
Trafilatura was more conservative on the same pages: 1,352 and 341. Neither is right for a page whose main content is a card grid, which is most vendor documentation hubs.
The pipeline that keeps tables
Run the prose extractor for the body, pull tables out of the raw HTML separately, append them as Markdown. Two dependencies, no schema guessing:
import io
import pandas as pd
import trafilatura
def extract_document(html: str) -> str:
body = trafilatura.extract(
html, include_tables=False, include_comments=False
) or ""
tables = []
try:
for df in pd.read_html(io.StringIO(html)):
if df.shape[0] >= 2 and df.shape[1] >= 2:
tables.append(df.to_markdown(index=False))
except ValueError:
pass # no tables on this page
parts = [body.strip()] + tables
return nn.join(p for p in parts if p)
pandas.read_html needs lxml and tabulate. On the five table pages above this recovered the full table text while keeping trafilatura body extraction, at roughly 1 to 40 ms per page. The output is Markdown, so the chunker sees pipe tables and the model sees column headers on every row.
Two checks worth adding before trusting either path:
- Count tokens before and after. A page whose extraction drops more than 40% of the naive token count is a candidate for manual inspection, not automatic approval.
- Verify the page's own h2 headings appear in the output. If the heading is missing, the extractor picked the wrong container, which is what happened on The Verge.
Does the 31.6% saving justify the work
On these 27 pages, 29,932 input tokens disappear with trafilatura. At one dollar per million input tokens that is three cents. At ten thousand pages a month the same ratio is roughly 11 million tokens, so the saving is real but it is not where a budget dies. The reason to strip boilerplate is retrieval quality: navigation text and cookie notices embed into vectors that then compete with the actual answer at query time.
The reason to keep tables is the same argument in reverse. A pipe table adds tokens and pays them back the first time a user asks for a number.
FAQ
Is trafilatura still the right default for article text? Yes for prose: it came within four percentage points of readability token cut while running faster and being less destructive on technical posts. Add table extraction and a token-count check and it is a solid default.
Why not BeautifulSoup get_text everywhere? Because 94,692 tokens against 64,760 is 46% more input on the same 27 pages, and a large share of the extra is menus, cookie banners and comment sections that pollute embeddings.
How to measure retention on a different corpus? Pull 20 URLs from a sitemap, extract the text inside pre and table elements from the raw HTML, compute 5-gram overlap against each extractor output. The script is 30 lines and it catches failures a spot check misses.
Related
- PDF to Markdown before RAG - the same retention question for Docling, Marker and MinerU.
- Sentence-aligned chunks beat 20% overlap - what to do with the extracted text next.
- Same 12 repos, 8 formats - how formatting decisions move the token bill.
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.