A pure Python HTML5 parser that just works.
Project description
JustHTML
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
Why use JustHTML?
-
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(pretty=False) # => <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 sanitization at construction time — built-in Bleach-style allowlist sanitization on
JustHTML(...)(disable withsanitize=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( "<div><p class=\"x\">Hi</p><p>Bye</p></div>", fragment=True, ).query_one("div p.x").to_html(pretty=False) # => <p class="x">Hi</p>
-
Just... Transform 🏗️ — Built-in DOM transforms for: drop/unwrap nodes, rewrite attributes, linkify text, and compose safe pipelines. (Transforms)
from justhtml import JustHTML, Linkify, SetAttrs, Unwrap doc = JustHTML( "<p>Hello <span class=\"x\">world</span> example.com</p>", transforms=[ Unwrap("span.x"), Linkify(), SetAttrs("a", rel="nofollow"), ], fragment=True, safe=False, ) print(doc.to_html(pretty=False)) # => <p>Hello world <a href="http://example.com" rel="nofollow">example.com</a></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)/usr/bin/time -f '%e s' bash -lc \ "curl -Ls https://en.wikipedia.org/wiki/HTML | python -m justhtml - > /dev/null" # 0.41 s
Comparison
| Tool | HTML5 parsing [1][2] | Speed | Query API | 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 | ❌ Needs sanitization | Fast but not HTML5 compliant. Don't use the old lxml.html.clean module! |
[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).
Installation
pip install justhtml
Next: Quickstart Guide, CSS Selectors, Sanitization & Security, or try the Playground.
Requires Python 3.10 or later.
Quick Example
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.
Command Line
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 --unsafe | head -n 8
Output:
# 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.
[📖 Full documentation](https://emilstenstrom.github.io/justhtml/) | [🛝 Try it in the Playground](https://emilstenstrom.github.io/justhtml/playground/)
Security
For security policy and vulnerability reporting, please see SECURITY.md.
Contributing
See CONTRIBUTING.md for development setup and guidelines.
Acknowledgments
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.
License
MIT. Free to use both for commercial and non-commercial use.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file justhtml-1.8.0.tar.gz.
File metadata
- Download URL: justhtml-1.8.0.tar.gz
- Upload date:
- Size: 363.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
951dd794ce5e6599a134c4af660b9c69d01142d8268418d660595249efdcb38d
|
|
| MD5 |
3718f836e33047414d743a4263c91832
|
|
| BLAKE2b-256 |
64ffbe9465ed7d7aedff3e5e9a7977d8d89ec8a9cd40294311763d70d0bff418
|
Provenance
The following attestation bundles were made for justhtml-1.8.0.tar.gz:
Publisher:
publish.yml on EmilStenstrom/justhtml
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
justhtml-1.8.0.tar.gz -
Subject digest:
951dd794ce5e6599a134c4af660b9c69d01142d8268418d660595249efdcb38d - Sigstore transparency entry: 1042949285
- Sigstore integration time:
-
Permalink:
EmilStenstrom/justhtml@fd60b6e7548b354d79969f7526a638093d7fcdab -
Branch / Tag:
refs/tags/v1.8.0 - Owner: https://github.com/EmilStenstrom
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fd60b6e7548b354d79969f7526a638093d7fcdab -
Trigger Event:
release
-
Statement type:
File details
Details for the file justhtml-1.8.0-py3-none-any.whl.
File metadata
- Download URL: justhtml-1.8.0-py3-none-any.whl
- Upload date:
- Size: 129.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4615d5bddc61f446b3ac72716985fb908fdfdf56c9772350a03f8e6debe8f9f
|
|
| MD5 |
b1ead8def6044dd8873a9f9b4d8b9e56
|
|
| BLAKE2b-256 |
b0b7879002b91425c374f25aff7794d6caa63e641df8fb954b53d890f49bbe94
|
Provenance
The following attestation bundles were made for justhtml-1.8.0-py3-none-any.whl:
Publisher:
publish.yml on EmilStenstrom/justhtml
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
justhtml-1.8.0-py3-none-any.whl -
Subject digest:
f4615d5bddc61f446b3ac72716985fb908fdfdf56c9772350a03f8e6debe8f9f - Sigstore transparency entry: 1042949296
- Sigstore integration time:
-
Permalink:
EmilStenstrom/justhtml@fd60b6e7548b354d79969f7526a638093d7fcdab -
Branch / Tag:
refs/tags/v1.8.0 - Owner: https://github.com/EmilStenstrom
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fd60b6e7548b354d79969f7526a638093d7fcdab -
Trigger Event:
release
-
Statement type: