lxml-html-clean
HTML cleaner from lxml project
What it is and what it does
lxml_html_clean is a standalone HTML sanitization library extracted from lxml's original cleaner module. It uses a blocklist-based approach to remove unwanted HTML tags and attributes from user-supplied or untrusted content. The package depends only on lxml and installs as a pure Python wheel.
The package explicitly warns that it is not suitable for security-sensitive environments. Its URL parsing relies on Python's urllib.parse, which does not validate inputs, and a maliciously crafted URL could bypass the allowed-hosts check. If you need robust HTML sanitization for high-security contexts, the maintainers recommend alternatives like nh3.
Use it for:
- Sanitize user-submitted HTML in a blog or comment system where moderate filtering suffices
- Remove script tags and event handlers from HTML copied from untrusted web sources
- Clean up HTML content for display in a non-security-critical web application
- Strip formatting and styling from HTML while preserving structure for content migration
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Cleans and sanitizes HTML by removing unwanted tags and attributes using a blocklist approach, extracted from lxml's original HTML cleaner module.
Yes, if you need basic HTML sanitization in a non-security-critical context. The package is actively maintained, has low install friction, and carries a permissive license. However, do not use it for security-sensitive applications—the maintainers explicitly recommend alternatives like nh3 for those cases. Verify that blocklist-based filtering meets your specific requirements.
Install
lxml-html-clean on PyPI
pip
pip install lxml-html-cleanuv
uv add lxml-html-cleanpoetry
poetry add lxml-html-cleanInstalling lxml-html-clean
Before you install
Low friction: pure Python wheel with a single runtime dependency on lxml. Actively maintained as of 2026-05-20 with recent releases.
License in practice
BSD-3-Clause permissive license allows commercial and private use with minimal restrictions; attribution required.
Quickstart
pip install lxml_html_clean
from lxml_html_clean import Cleaner
cleaner = Cleaner()
clean_html = cleaner.clean_html('<p>Hello <script>alert(1)</script></p>')
Requires lxml, which may need compilation on some systems. Not recommended for security-sensitive applications—see package documentation for alternatives.
Verify before relying
- Whether the blocklist approach provides adequate protection for your specific use case, given the package's own warning against security-sensitive environments
- Whether URL parsing via urllib.parse's non-validating functions poses a risk for your allowed-hosts configuration
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — lxml |
| Maintenance | actively maintained — 86 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 20,471,755/month — #1,036 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: lxml_html_clean-0.4.5-py3-none-any.whl
Tags
More HTML packages
MarkupSafe provides a text object that escapes…
permissive · top 100 on PyPI
Jinja2Jinja2 is a templating engine that renders…
permissive · top 100 on PyPI
beautifulsoup4Beautiful Soup parses HTML and XML documents…
permissive · top 100 on PyPI
lxmllxml provides Python bindings to libxml2 and…
permissive · top 1,000 on PyPI
docutilsDocutils converts plaintext documentation in…
unclear · top 1,000 on PyPI
MarkdownConverts Markdown text to HTML using a Python…
permissive · top 1,000 on PyPI
django-bleachIntegrates bleach HTML sanitization into Django…
permissive · top 15,000 on PyPI
nh3nh3 sanitizes HTML by removing unsafe tags and…
permissive · top 1,000 on PyPI
bleachBleach sanitizes untrusted HTML by escaping or…
permissive · top 1,000 on PyPI
bleach-allowlistProvides curated allowlists of HTML tags,…
permissive · top 5,000 on PyPI
py_svg_hushSanitizes SVG files by removing malicious…
unclear · top 15,000 on PyPI
html-sanitizerSanitizes HTML fragments by enforcing an…
permissive · top 5,000 on PyPI
disposable-email-domainsProvides a Python set of known disposable email…
permissive · top 5,000 on PyPI
git-filter-repoRewrites git repository history efficiently,…
permissive · top 5,000 on PyPI
htmlmin2Minifies HTML by removing unnecessary…
permissive · top 5,000 on PyPI