HTML to Markdown in Python: markdownify vs html2text vs html-to-markdown vs MarkItDown
For HTML to markdown in Python, a library does the job when the HTML is already in hand. On 10 real pages, html-to-markdown was the fastest (median 9 ms per page), markdownify and MarkItDown produced nearly the same output, and html2text was fastest of the older libraries but dropped the most links and tables. None of the four removes navigation, footers or cookie text. That part needs a selector or an extraction step. If the HTML is not in hand yet, the web scraping API fetches the page and returns markdown directly.
This matters when crawled pages feed a support bot or an AI knowledge base. Every leaked footer costs tokens on each embedding and prompt, and boilerplate repeated across pages can pull retrieval toward the wrong chunk.
This is the Python counterpart of HTML to markdown in JavaScript, and it sits inside the larger website to markdown knowledge base pipeline. All numbers below come from one run on 2026-10-29: 10 pages, 4 libraries, one Apple Silicon laptop (Darwin arm64, Python 3.11.2).
Copy-paste answer
from markdownify import markdownify
md = markdownify(html, heading_style="ATX")
print(md)
With html-to-markdown the same call is one line: html_to_markdown.convert(html).content. With html2text it is html2text.html2text(html). That is enough for a clean article body. The rest of this post covers what breaks.
HTML to markdown in Python: results at a glance
Input is the full page HTML. Time is the median across the 10 pages of each page's median over 20 runs. Recall is the share of the LLM-labeled main words found in the output. Leakage is the share of output tokens that are neither main nor borderline text in that labeling (method below).
| Library | Median ms/page | Median tokens/page | Leakage (pooled) | Recall (mean) | Tables kept | Code blocks (exact) | Unique link targets kept (avg per page) |
|---|---|---|---|---|---|---|---|
| markdownify (ATX) | 52 | 12,090 | 7.5% | 99.9% | 27 of 27 | 322 of 353 | 93.7% |
| html2text (body_width=0) | 22 | 11,253 | 7.4% | 99.9% | 23 of 27 | 234 of 353 | 70.6% |
| html-to-markdown | 9 | 13,145 | 8.0% | 99.4% | 26 of 27 | 323 of 353 | 99.1% |
| MarkItDown | 63 | 12,076 | 7.4% | 99.9% | 27 of 27 | 321 of 353 | 92.7% |
| WebCrawlerAPI /markdown (10 calls) | not compared | 8,179 | 1.5% | 95.5% | not scored | not scored | not scored |
Tables, code and links were scored on the pre-extracted main region, so the input is the same for every library. The API row is compared on tokens, recall and leakage only, for reasons given in the API section.

Three verdicts from the data:
- Speed: html-to-markdown is about 6x faster than markdownify.
- Output: markdownify and MarkItDown are the same engine and score the same.
- Leakage: every library lets 7% to 8% of tokens through from outside the main content. Pre-extracting the region cut that to about 3.6% for three of them. The API leaked 1.5%, at the price of 95.5% mean recall.
Reruns moved medians by around 10%, so read small gaps as ties.
How the benchmark was run
Versions came from pip freeze in a venv: markdownify 1.2.3, html2text 2025.4.15, html-to-markdown 3.19.0, markitdown 0.1.9, tiktoken 0.14.0.
Each page was fetched once, after a robots.txt check, with a descriptive User-Agent and a 3 second pause. The HTML was cached on disk, so the timings contain no network. The API calls were made right after the fetch, so both sides saw the pages within about two minutes of each other.
| Type | Page (URL) | Fetched |
|---|---|---|
| Docs | docs.python.org/3/tutorial/classes.html | 2026-10-29 |
| Docs | requests.readthedocs.io/en/latest/user/quickstart/ | 2026-10-29 |
| Docs | flask.palletsprojects.com/en/stable/quickstart/ | 2026-10-29 |
| Table-heavy | en.wikipedia.org/wiki/Comparison_of_lightweight_markup_languages (47 cells with colspan or rowspan on the page) | 2026-10-29 |
| Table-heavy | docs.python.org/3/library/functions.html | 2026-10-29 |
| Blog | martinfowler.com/articles/microservices.html | 2026-10-29 |
| Blog | joelonsoftware.com/2000/08/09/the-joel-test-12-steps-to-better-code/ | 2026-10-29 |
| Code-heavy | docs.python.org/3/library/asyncio-task.html (highlighted spans) | 2026-10-29 |
| Code-heavy | go.dev/doc/effective_go (153 <pre> blocks) | 2026-10-29 |
| Messy | theguardian.com/technology | 2026-10-29 |
robots.txt was checked for every host before the fetch and allowed all 10 paths. All pages were fetched between 13:16:09 and 13:16:47 UTC.
Metrics, one line each:
- Time: 1 warmup plus 20 runs per page per library, in-process. Import time is measured separately.
- Tokens: cl100k_base only, counted on the full output.
- Tables: source <table> count and rows against pipe tables and rows in the output.
- Code: source <pre> blocks against fenced blocks, and whether the text and indentation survived.
- Links: source <a href> against [text](url).
- Leakage: see below. The unit is tokens, chosen before running.
For each page the text was labeled by an LLM in three lists: main (what an AI knowledge base should keep), not main (navigation, footers, banners, promos) and borderline (boxed sidebars, footnotes, "Further reading", bylines and dates, where the right call depends on the use). On the clean docs and blog-style pages one rule was enough: everything inside the content element is main, plus a list of exceptions to remove. On Fowler, Wikipedia and the Guardian the main sections were listed heading by heading.
Output words are matched against the lists as a multiset, after dropping link targets and undoing markdown escapes. Recall is the share of main words found in the output. Leakage is the token share of output words that are not main or borderline: words from the not-main lists, plus words in neither list, which includes converter noise such as base64 image text. Borderline text is reported separately (the libraries kept 3.0% to 3.3% of their tokens as borderline, the API 1.9%) and counts neither as recall nor as leakage. This is one person's labeling over 10 pages with a text-match heuristic. It is not a formal extraction metric. A word present in both navigation and the article is absorbed by the article count, so leakage is slightly undercounted.
Underscores are not counted as word characters, because html2text writes emphasis as _word_ and would otherwise look leakier than it is. The method follows Python HTML parsers compared. The core of the run is below.
converters = {
"markdownify": lambda h: markdownify.markdownify(h, heading_style="ATX"),
"html2text": lambda h: html2text_nowrap(h), # HTML2Text with body_width = 0
"html-to-markdown": lambda h: html_to_markdown.convert(h).content,
"markitdown": lambda h: md_it.convert_stream(io.BytesIO(h.encode()), stream_info=info).markdown,
}
for name, fn in converters.items():
fn(html) # warmup
times = [timed(fn, html) for _ in range(20)]
md = fn(html)
tokens = len(tiktoken.get_encoding("cl100k_base").encode(md))
The four libraries
markdownify
markdownify walks a BeautifulSoup tree. Defaults emit setext headings (underlined), and heading_style="ATX" switches to #. The token difference was 98 of 146,063 across all pages. It kept 27 of 27 tables, but an empty header row is added when the source has no <thead> (229 output rows for 219 source rows).
html2text
The default wraps lines at 78 columns. Setting body_width = 0 cut total tokens from 140,391 to 136,643 (2.7%) and median time from 28 ms to 22 ms. It writes code as 4-space indented blocks (1 fence in 353 blocks) and writes tables as a | b rows with a ---|--- separator and no outer pipes. It drops links that point to a #fragment on the same page (its skip_internal_links default is on), which covers most table-of-contents links on docs pages. That is why only 70.6% of unique link targets survived.
html-to-markdown
The import name is html_to_markdown and the call is html_to_markdown.convert(html).content. It is maintained under the xberg-io GitHub organization (Kreuzberg). It is Rust-backed: the wheel is built with maturin, ships a compiled _html_to_markdown.abi3.so, and the binary contains pyo3 and Cargo crate paths.
Two defaults cost tokens. It prepends YAML frontmatter with page metadata, and it turns inline <svg> into base64 data:image/svg+xml images. The Guardian page produced 25,095 tokens with defaults and 7,386 with extract_metadata=False, strip_tags=["svg"] (the "tuned" variant). Across all 10 pages the tuned total is 138,724 tokens against 158,402 by default. A third default causes real damage: on Fowler's article it dropped 5.8% of the labeled main words, because its preprocessing removes elements with the class sidebar, and the article keeps real content in <aside class='sidebar'> boxes.
markitdown python
MarkItDown (Microsoft) is built to convert many file types and takes a file or stream, not a string. For HTML it parses with BeautifulSoup (html.parser), removes script and style, and runs a markdownify.MarkdownConverter subclass on <body>. This was checked by reading the installed 0.1.9 source (markitdown/converters/_html_converter.py and _markdownify.py). The output was within 0.3% of markdownify in tokens, and table and code scores match to within one block.
The extra cost is startup: importing and creating MarkItDown() took 559 ms in the benchmark venv (installed with extras) and about 190 ms in a clean venv with only pip install markitdown (markdownify: about 40 ms in the same venv).
Results
Speed
| Library | Median ms/page | p95 ms (all runs) | Total for 10 pages | Import |
|---|---|---|---|---|
| markdownify | 52 | 190 | 0.72 s | 49 ms |
| html2text (body_width=0) | 22 | 67 | 0.30 s | 8 ms |
| html-to-markdown | 9 | 29 | 0.12 s | 19 ms |
| MarkItDown | 63 | 226 | 0.86 s | 559 ms |
The slowest page was the 331 KB Python built-in functions reference (190 ms in markdownify, 29 ms in html-to-markdown). Fetching the same 10 pages took a median of 127 ms each (63 to 436 ms). In a crawl the fetch dominates, so conversion speed matters for bulk jobs over cached HTML.
Tables
27 <table> elements and 219 rows sat in the labeled regions, 23 of them on the Wikipedia page. Cell text survived everywhere (99.6% to 100% of cells found). The shape did not.

The Flask "converter types" table has 5 rows and no header. markdownify and MarkItDown add an empty header row. html-to-markdown and html2text promote string to the header. For the Python functions table, whose header cell spans 4 columns, markdownify emits | Built-in Functions | | | |, and html2text emits no table at all, only text. html2text kept 23 of 27 tables as tables with a separator row. For html table to markdown work, markdownify and MarkItDown are the safest.
Code blocks and links
There were 353 <pre> blocks. markdownify, html-to-markdown and MarkItDown wrote 335 fenced blocks each, and none of those fences carried a language tag, even though the Sphinx pages mark the language on a wrapper class. Exact text and indentation matched on 322, 323 and 321 blocks. html2text indents code instead of fencing it and matched 234 exactly, though the text was found in 350.
Links: 1,866 <a href> in the regions. html-to-markdown kept 99.1% of unique targets, markdownify 93.7%, MarkItDown 92.7% and html2text 70.6%. The misses for the first three were under 8% and not every one was traced.
Tokens
For HTML against markdown token counts on 20 sites, see how many tokens a web page is.
Totals over the 10 full pages, cl100k_base: html2text without wrapping 136,643, tuned html-to-markdown 138,724, html2text default 140,391, markdownify (ATX) 145,965, MarkItDown 146,450 and html-to-markdown with defaults 158,402. Differences come from wrapping, escaping (markdownify writes FIRST\_COMPLETED), image syntax and the two html-to-markdown defaults. See HTML vs markdown as an output format for why markdown is worth the conversion.
The problem none of them fix: boilerplate
Pooled over 10 pages, between 7.4% and 8.0% of output tokens were not main or borderline text (6.1% for tuned html-to-markdown). Medians are lower (5.5% to 7.3%) because one page drags the pool. On the Guardian front page 69% to 73% of tokens were navigation, footers, promos and ad text, with only about 480 words of labeled main text on the page.
Pre-extracting the region with select_one(selector) and converting only that cut pooled leakage to 3.6% for markdownify, html2text and MarkItDown, and 4.3% for html-to-markdown. Most of what remains comes from two pages: Fowler, whose contents list and author bios sit inside main, and the Guardian, whose promo and ad blocks sit inside main. Median tokens fell from 12,090 to 10,962 for markdownify.
The catch is the selector: one per site, written by hand, broken by markup changes. The 10 pages from 8 sites needed 4 distinct selectors, and each page had to be opened to pick one. For a crawl of 500 domains it is a maintenance job.
Same pages through the cleaning API
The same 10 URLs went through POST /markdown, one call per page, 10 calls in total. The endpoint scrapes the page and runs an LLM pass that keeps only the main content. It takes only a url:
curl --request POST \
--url https://api.webcrawlerapi.com/markdown \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{"url": "https://martinfowler.com/articles/microservices.html"}'
import requests
r = requests.post(
"https://api.webcrawlerapi.com/markdown",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"url": url},
)
markdown = r.json()["markdown"]
For plain page markdown without the LLM step, POST /v2/scrape with output_formats: ["markdown"] is cheaper. This test did not run it.
The comparison has caveats. The fetch for the libraries ran at 13:16 UTC and the API calls followed at 13:16 to 13:17 UTC, so a headline page like the Guardian had at most a minute or two to change. The API renders the page itself, while the libraries read the raw HTML, so the two inputs are not byte-identical. For that reason the API is compared on tokens, recall and leakage only, not on tables, code or links. /markdown has no respect_robots_txt parameter, so robots.txt handling for these calls was not set by the request.
| Tokens (10 pages) | Leakage (pooled) | Main words found (mean) | Main words found (median) | |
|---|---|---|---|---|
| WebCrawlerAPI /markdown | 118,994 | 1.5% | 95.5% | 99.7% |
| Best library, full page (tuned html-to-markdown) | 138,724 | 6.1% | 99.4% | 100% |
| markdownify (ATX headings) | 145,965 | 7.5% | 99.9% | 100% |

The API removed most boilerplate: 1.5% pooled leakage against 6.1% to 8.0% for the libraries on full pages, and also below the 3.6% that a hand-picked selector gave markdownify. The price is recall, and it is uneven. Eight of 10 pages kept 97.9% to 100% of the labeled main words. Fowler's article kept 77.6% and the Guardian front page 80.9%. On Fowler the output has no boxed sidebars, no references and no "Further Reading" section. The labels put the sidebars in borderline and the references in main, so that second call alone moves the score by about 4.5 points (82.1% without the references). The Guardian page is a list of stories, and the LLM cut some of them. It also leaked 32% of its Guardian tokens, so it was not clean on that page either. If your labeling counts sidebars, references or teaser lists as main content, the API trades recall for cleanliness.
It returned no images in any of the 10 responses. It also glued a kicker into a headline on the Guardian page ("UKAI bots flooded..."), a defect the libraries did not have.
Latency was a median of 3.4 s per page (2.6 to 6.7 s, the slowest being the 23-table Wikipedia page), against 9 to 63 ms for conversion alone. That is not a like-for-like comparison, because the API fetches, renders and cleans the page. The response carries no cost field. By the pricing docs (read 2026-10-29) /markdown costs $0.006 per page on pay as you go, which makes 10 pages $0.06.
A library with a hand-picked selector keeps more of the text for free: pre-extracted markdownify leaked 3.6% pooled at 99.9% recall. The API does the picking for you on any site.
Which one to use
- HTML in hand, bulk conversion, offline: html-to-markdown, with extract_metadata=False and strip_tags=["svg"]. Check that your pages do not put content in elements with a sidebar class.
- Smallest surprises, tables matter: markdownify with heading_style="ATX".
- Already using MarkItDown for PDFs and Office files: it is fine for HTML, with the same output as markdownify.
- html2text: only for plain-text-style output. It lost 4 of 27 tables and 29% of unique link targets here.
- Known site, one selector, local control: a library plus select_one. It keeps more of the text than the API and costs nothing per page.
- You need the fetch, JavaScript rendering and cleaning anyway, across many sites: POST /markdown returns cleaned main-content markdown in one call, at the cost of some recall on list-style pages. /tools/url-to-md does a single page without code, and the web scraping API page has the full set.
Next steps
- Crawl a whole site to files: Crawl a website to markdown files.
- Cut the output into pieces: Chunking strategies for a knowledge base.
- End to end, with URL input, PDFs and JavaScript pages: MarkItDown vs Docling vs Jina Reader vs Crawl4AI.
- The JavaScript side of the same problem: HTML to markdown in JavaScript.
- Get an API key at webcrawlerapi.com/crawl-api and read the /markdown docs. The web scraping API page covers single pages.
