A pure Python HTML5 parser that just works. No C extensions to compile. No system dependencies to install. No complex API to learn.
π Full documentation | π Try it in the Playground
-
Just... Correct β β Spec-perfect HTML5 parsing with browser-grade error recovery β passes the official 9k+ html5lib-tests suite, with 100% line+branch coverage. (Correctness)
JustHTML("<p><b>Hi<i>there</b>!", fragment=True).to_html() # => <p><b>Hi<i>there</i></b><i>!</i></p> # Note: fragment=True parses snippets (no <html>/<body> needed)
-
Just... Python π β Pure Python, zero dependencies β no C extensions or system libraries, easy to debug, and works anywhere Python runs, including PyPy and Pyodide. (Run in the browser)
python -m pip show justhtml | grep -E '^Requires:' # Requires: [intentionally left blank]
-
Just... Secure π β Safe-by-default output for untrusted HTML β built-in Bleach-style allowlist sanitization on
to_text(),to_html(),to_markdown()(override withsafe=False). Can sanitize inline CSS rules. (Sanitization & Security)JustHTML( "<p>Hello<script>alert(1)</script> " "<a href=\"javascript:alert(1)\">bad</a> " "<a href=\"https://example.com/?a=1&b=2\">ok</a></p>", fragment=True, ).to_html() # => <p>Hello <a>bad</a> <a href="https://example.com/?a=1&b=2">ok</a></p>
-
Just... Query π β CSS selectors out of the box β one method (
query()), familiar syntax (combinators, groups, pseudo-classes), and plain Python nodes as results. (CSS Selectors)JustHTML( "<main><p class=\"x\">Hi</p><p>Bye</p></main>", fragment=True, ).query("main p.x")[0].to_html() # => <p class="x">Hi</p>
-
Just... Fast Enough β‘ β Fast for the common case (fastest pure-Python HTML5 parser available); for terabytes, use a C/Rust parser like
html5ever. (Benchmarks)TIMEFORMAT='%3R s' time curl -Ls https://en.wikipedia.org/wiki/HTML \ | python -m justhtml - > /dev/null # 0.365 s
| Tool | HTML5 parsing [1][2] | Speed | CSS query | Sanitizes output | Notes |
|---|---|---|---|---|---|
| JustHTML Pure Python |
β Β 100% | β‘ Fast | β CSS selectors | β
Built-in (safe=True) |
Correct, easy to install, and fast enough. |
| Chromium browser engine |
β 99% | πΒ VeryΒ Fast | β | β | β |
| WebKit browser engine |
β 98% | π Very Fast | β | β | β |
| Firefox browser engine |
β 97% | π Very Fast | β | β | β |
html5libPure Python |
π‘ 88% | π’ Slow | π‘ XPath (lxml) | π΄ Deprecated | Unmaintained. Reference implementation; Correct but quite slow. |
html5_parserPython wrapper of C-based Gumbo |
π‘ 84% | π Very Fast | π‘ XPath (lxml) | β Needs sanitization | Fast and mostly correct. |
selectolaxPython wrapper of C-based Lexbor |
π‘ 68% | π Very Fast | β CSS selectors | β Needs sanitization | Very fast but less compliant. |
html.parserPython stdlib |
π΄ 4% | β‘ Fast | β None | β Needs sanitization | Standard library. Chokes on malformed HTML. |
BeautifulSoupPure Python |
π΄ 4% (default) | π’ Slow | π‘ Custom API | β Needs sanitization | Wraps html.parser (default). Can use lxml or html5lib. |
lxmlPython wrapper of C-based libxml2 |
π΄ 1% | π Very Fast | π‘ XPath | π΄ Not considered safe | Fast but not HTML5 compliant. |
[1]: Parser compliance scores are from a strict run of the html5lib-tests tree-construction fixtures (1,743 non-script tests). See docs/correctness.md for details.
[2]: Browser numbers are from justhtml-html5lib-tests-bench on the upstream html5lib-tests/tree-construction corpus (excluding 12 scripting-enabled cases).
pip install justhtmlNext: Quickstart Guide, CSS Selectors, Sanitization & Security, or try the Playground.
Requires Python 3.10 or later.
from justhtml import JustHTML
doc = JustHTML("<html><body><p class='intro'>Hello!</p></body></html>")
# Query with CSS selectors
for p in doc.query("p.intro"):
print(p.name) # "p"
print(p.attrs) # {"class": "intro"}
print(p.to_html()) # <p class="intro">Hello!</p>See the Quickstart Guide for more examples including tree traversal, streaming, and strict mode.
If you installed JustHTML (for example with pip install justhtml or pip install -e .), you can use the justhtml command.
If you don't have it available, use the equivalent python -m justhtml ... form instead.
# Pretty-print an HTML file
justhtml index.html
# Parse from stdin
curl -s https://example.com | justhtml -
# Select nodes and output text
justhtml index.html --selector "main p" --format text
# Select nodes and output Markdown (subset of GFM)
justhtml index.html --selector "article" --format markdown
# Select nodes and output HTML
justhtml index.html --selector "a" --format html# Example: extract Markdown from GitHub README HTML
curl -s https://github.com/EmilStenstrom/justhtml/ | justhtml - --selector '.markdown-body' --format markdown | head -n 15Output:
# JustHTML
[](#justhtml)
A pure Python HTML5 parser that just works. No C extensions to compile. No system dependencies to install. No complex API to learn.
**[π Read the full documentation here](/EmilStenstrom/justhtml/blob/main/docs/index.md)**
## Why use JustHTML?
- **Just... Correct β
** β Spec-perfect HTML5 parsing with browser-grade error recovery β passes the official 9k+ [html5lib-tests](https://github.com/html5lib/html5lib-tests) suite, with 100% line+branch coverage. ([Correctness](/EmilStenstrom/justhtml/blob/main/docs/correctness.md))
- **Just... Python π** β Pure Python, zero dependencies β no C extensions or system libraries, easy to debug, and works anywhere Python runs (including PyPy and Pyodide). ([Quickstart](/EmilStenstrom/justhtml/blob/main/docs/quickstart.md))
- **Just... Secure π** β Safe-by-default output for untrusted HTML β built-in Bleach-style allowlist sanitization on `to_html()` / `to_markdown()` (override with `safe=False`), plus URL/CSS rules. ([Sanitization & Security](/EmilStenstrom/justhtml/blob/main/docs/sanitization.md))
For security policy and vulnerability reporting, please see SECURITY.md.
See CONTRIBUTING.md for development setup and guidelines.
JustHTML started as a Python port of html5ever, the HTML5 parser from Mozilla's Servo browser engine. While the codebase has since evolved significantly, html5ever's clean architecture and spec-compliant approach were invaluable as a starting point. Thank you to the Servo team for their excellent work.
Correctness and conformance work is heavily guided by the html5lib ecosystem and especially the official html5lib-tests fixtures used across implementations.
The sanitization API and threat-model expectations are informed by established Python sanitizers like Bleach and nh3.
The CSS selector query API is inspired by the ergonomics of lxml.cssselect.
MIT. Free to use both for commercial and non-commercial use.