Skip to main content

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... Secure 🔒

Safe-by-default sanitization at construction time. Built-in Bleach-style allowlist sanitization on JustHTML(...) (disable with sanitize=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&amp;b=2">ok</a></p>

Just... Query 🔍

CSS selectors out of the box. Two methods (query(), query_one()), 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>

⚠️ Note: Sanitization happens before querying, so remember to disable (sanitize=False) if you are working with safe HTML.

Just... Transform 🏗️

Built-in DOM transforms for dropping and unwrapping nodes, rewriting attributes, linkifying text, and composing 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... Build 🧱

Build node trees directly when Python is driving the HTML, then normalize them through the same HTML5 parser. (Building HTML)

from justhtml import JustHTML
from justhtml.builder import element

doc = JustHTML(
    element(
        "article",
        {"class": "post"},
        element("h2", "JustHTML"),
        element("p", "Build nodes directly."),
        element("a", {"href": "/docs"}, "Read docs"),
    ),
    fragment=True,
    sanitize=False,
)
print(doc.to_html(pretty=False))
# => <article class="post"><h2>JustHTML</h2><p>Build nodes directly.</p><a href="/docs">Read docs</a></article>

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... Fast Enough ⚡

Fast for the common case, and the fastest pure-Python HTML5 parser available. For terabytes, use a C/Rust parser like html5ever. (Benchmarks)

curl -Ls https://en.wikipedia.org/wiki/HTML -o /tmp/justhtml-bench.html
/usr/bin/time -f '%e s' python -m justhtml /tmp/justhtml-bench.html > /dev/null
# 0.22 s

Comparison

Tool HTML5 parsing [1][2] Speed Query Build Sanitize Notes
JustHTML
Pure Python
✅ 100% ⚡ Fast ✅ CSS selectors element() ✅ Built-in Correct, secure, easy to install, and fast enough.
Chromium
browser engine
99% 🚀 Very Fast
WebKit
browser engine
98% 🚀 Very Fast
Firefox
browser engine
97% 🚀 Very Fast
markupever
Python wrapper of Rust-based html5ever
95% 🚀 Very Fast ✅ CSS selectors TreeDom. create_*() ❌ Needs sanitization Fast and correct.
html5lib
Pure Python
🟡 88% 🐢 Slow 🟡 XPath (lxml) 🟡 Tree API 🔴 Deprecated Unmaintained. Reference implementation; Correct but quite slow.
html5_parser
Python wrapper of C-based Gumbo
🟡 84% 🚀 Very Fast 🟡 XPath (lxml) 🟡 etree (lxml) ❌ Needs sanitization Fast and mostly correct.
selectolax
Python wrapper of C-based Lexbor
🟡 68% 🚀 Very Fast ✅ CSS selectors create_node() ❌ Needs sanitization Very fast but less compliant.
BeautifulSoup
Pure Python
🔴 5% (default) 🐢 Slow 🟡 Custom API new_tag() API ❌ Needs sanitization Wraps html.parser (default). Can use lxml or html5lib.
html.parser
Python stdlib
🔴 4% ⚡ Fast ❌ None ❌ None ❌ Needs sanitization Standard library. Chokes on malformed HTML.
lxml
Python wrapper of C-based libxml2
🔴 3% 🚀 Very Fast 🟡 XPath etree / E-factory ❌ 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

justhtml-1.14.0.tar.gz (381.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

justhtml-1.14.0-py3-none-any.whl (135.9 kB view details)

Uploaded Python 3

File details

Details for the file justhtml-1.14.0.tar.gz.

File metadata

  • Download URL: justhtml-1.14.0.tar.gz
  • Upload date:
  • Size: 381.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for justhtml-1.14.0.tar.gz
Algorithm Hash digest
SHA256 372e0a3ffc847605eeedaf0fc587c6cae5f19a6ff705a5d466fece036ac1fa5e
MD5 9d17bbf5abad2b7769d454a27508c587
BLAKE2b-256 6d7c2e8784d2de43746c93b2b35490dc9d026b75b380bed516703d1faaccb8d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for justhtml-1.14.0.tar.gz:

Publisher: publish.yml on EmilStenstrom/justhtml

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file justhtml-1.14.0-py3-none-any.whl.

File metadata

  • Download URL: justhtml-1.14.0-py3-none-any.whl
  • Upload date:
  • Size: 135.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for justhtml-1.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7dd52f0a359d3802c7dad0b49beee5dbbfab2c6cda3561c8dad5909e0aa8a68d
MD5 e209e567356e5799fc9116bb5ea281bf
BLAKE2b-256 3f8465535499360c0df90d615d11a9ee4877fc2c76719d5c3aca3b55a0e67743

See more details on using hashes here.

Provenance

The following attestation bundles were made for justhtml-1.14.0-py3-none-any.whl:

Publisher: publish.yml on EmilStenstrom/justhtml

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page