lxml

A fast, feature-rich Python library for processing XML and HTML, built on libxml2 and libxslt.

Library
PyPI
v6.1.3
3,067 stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
89 /100 Excellent
Development Activity 96
Maintenance 72
Community 88
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
84 /100 Excellent
Architecture 80
Code Quality 85
Innovation 85
Learning Curve 85

lxml is a Python binding for the libxml2 and libxslt C libraries, giving Python code access to industrial-strength XML and HTML parsing, validation, XPath, and XSLT without sacrificing speed. Its lxml.etree module mirrors the standard library’s ElementTree API, so existing code can adopt it with minimal changes while gaining access to full XPath 1.0 expressions, XSLT transformations, RelaxNG and XML Schema validation, and C14N canonicalization.

Beyond the ElementTree-compatible core, lxml ships lxml.html for lenient real-world HTML parsing and cleanup, lxml.objectify for attribute-style data-binding access to XML trees, and a soup-parser mode for recovering structure from badly formed markup. With over 250 million monthly downloads from PyPI, it is a de facto standard wherever Python needs to parse, generate, or transform XML and HTML at scale.

What You Get

  • An lxml.etree API that is a drop-in superset of the standard library’s xml.etree.ElementTree, easing migration
  • Full XPath 1.0 and XSLT 1.0 support for querying and transforming documents
  • RelaxNG, XML Schema (XSD), and DTD validation built on libxml2’s native validators
  • lxml.html for parsing and cleaning real-world, often malformed HTML documents
  • lxml.objectify for attribute-style, data-binding access to XML trees
  • C14N canonicalization and incremental/iterative parsing (iterparse) for large documents

Common Use Cases

  • Scraping and cleaning HTML from live websites where markup is malformed or inconsistent
  • Parsing and validating large XML feeds or documents (e.g. SOAP, RSS, sitemaps) against XSD/RelaxNG schemas
  • Running XSLT transformations to convert XML into HTML, other XML dialects, or plain text
  • Extracting data from documents with XPath expressions instead of manual tree traversal
  • High-throughput XML processing where the pure-Python xml.etree.ElementTree is too slow

Under The Hood

Architecture

lxml is a thin, tightly-integrated Cython layer over the libxml2 and libxslt C libraries rather than a pure-Python tree implementation. The compiled core lives in src/lxml/etree.pyx, a single large extension module assembled by #include-ing dozens of .pxi fragments (parser.pxi, xslt.pxi, xmlschema.pxi, xpath.pxi, proxy.pxi, apihelpers.pxi, and others) so the whole surface compiles into one shared object for performance. The includes/ directory holds .pxd files that declare the libxml2/libxslt C API to Cython, and proxy.pxi handles the Python/C object proxying that lets Python-level Element objects wrap underlying libxml2 tree nodes safely. Higher-level, pure-Python modules — objectify.pyx, sax.py, builder.py, cssselect.py, and the html/ subpackage — sit on top of this compiled core and delegate the actual parsing, validation, and transformation work down to libxml2/libxslt via direct C calls. Because so much is compiled into one module by design, changes to shared internals like apihelpers.pxi or proxy.pxi ripple across the whole library, but the API surface itself is cleanly partitioned by concern (etree, html, objectify, sax).

Tech Stack

The project targets Python 3.9+ and is built with Cython 3.3+ against statically or dynamically linked libxml2 and libxslt (pinned versions such as libxml2 2.14.6 and libxslt 1.1.43 for released wheels, configured via cibuildwheel). The build itself is a custom setup.py/setupinfo.py/versioninfo.py pipeline rather than a declarative pyproject.toml metadata block, reflecting the complexity of compiling against a C toolchain across many platforms and architectures (x86_64, aarch64, i686, ppc64le, armv7l, riscv64, plus Windows and macOS universal2 builds). Optional extras integrate cssselect, html5lib, and BeautifulSoup4 for CSS-selector queries and HTML5-compliant or soup-style parsing. CI and release wheels are produced through GitHub Actions and cibuildwheel.

Code Quality

The src/lxml/tests directory contains a large, long-standing unittest-based suite (test_etree.py, test_html5parser.py, test_elementtree.py, test_dtd.py, test_xpathevaluator.py, and dozens more) alongside doctest-driven tests exercised via doc/*.txt files and a doctestcompare.py helper, plus fuzz-testing entry points (fuzz_xml_parse.py). With over 7,100 commits and 44 tracked releases, the project shows a long, consistently maintained history. Type safety is enforced at the Cython level through .pxd declarations rather than Python type hints, and correctness of the C-library bindings is validated continuously through the extensive test matrix run across build targets in CI.

API Design lxml’s central design decision is API compatibility: lxml.etree mirrors the standard library’s ElementTree interface closely enough that many programs can switch by changing an import, then opt into XPath, XSLT, and schema validation as needed rather than reaching for separate libraries. This consolidates functionality — parsing, querying, transforming, validating, and HTML cleanup — that in other language ecosystems is typically split across several packages. The tradeoff is a real learning curve for the C-library-backed pieces of the API (memory management semantics, thread-safety considerations, the objectify data-binding style) that go beyond basic tree manipulation, though the extensive doc/ directory (FAQ, performance notes, parsing guide, XPath/XSLT guide) and README are aimed squarely at bridging that gap.

Used by 29 apps in this directory

HTML
84%
Apache 2.0

Elementary

Analytics · Data Engineering · Monitoring

2,428

The dbt-native data observability CLI that turns your existing dbt tests and metadata into anomaly detection, lineage graphs, and Slack/Teams alerts — no separate platform required.

View details
88
Repo Health
75
Technical
75
Dependency
Built with
HTML 84%
Python 16%
Updated 1 weeks ago
TypeScript
51%
AGPL 3.0

Glean

Bookmarks Archiving · Knowledge Management

872

Self-hosted RSS reader and personal knowledge management tool with MCP server integration for AI assistant connectivity.

View details
73
Repo Health
75
Technical
70
Dependency
Built with
TypeScript 51%
Python 45%
Updated 1 weeks ago
Python
71%
Apache 2.0

GPT Researcher

AI Assistants · Productivity

30,269

The pioneering open-source autonomous AI agent that conducts deep, multi-source research and produces citation-backed reports exceeding 2,000 words — faster and more reliably than any human researcher.

View details
90
Repo Health
91
Technical
64
Dependency
Built with
Python 71%
TypeScript 18%
Updated 2 weeks ago
HTML
45%
LGPL-2.1

Horilla

ERP · Human Resources

1,470

Open-source HRMS covering recruitment, attendance, payroll, and biometrics in one self-hosted Django application.

View details
91
Repo Health
60
Technical
64
Dependency
Built with
HTML 45%
Python 41%
JavaScript 11%
Updated 1 weeks ago
Python
51%
AGPL 3.0

Khoj

AI Assistants · Knowledge Management · Productivity

37,802

A self-hostable AI second brain that chats with your documents, searches the web, builds custom agents, and runs entirely on your own LLM.

View details
62
Repo Health
82
Technical
66
Dependency
Built with
Python 51%
TypeScript 36%
Updated 2 months ago
Python
94%
Apache 2.0

knowhere

AI Development · AI Memory · Developer Tools

3,755

Transform messy, unstructured documents into persistent, navigable memory that AI agents can actually use.

View details
83
Repo Health
75
Technical
66
Dependency
Built with
Python 94%
Updated 6 days ago
TypeScript
39%
Apache 2.0

Label Studio

AI Development · Data Engineering

28,540

Label Studio is an open-source, multi-type data labeling platform that lets teams annotate images, text, audio, video, and time series data with a configurable XML-based UI and export annotations in formats ready for any ML framework.

View details
93
Repo Health
87
Technical
64
Dependency
Built with
TypeScript 39%
JavaScript 27%
Python 25%
Updated 1 weeks ago
C++
66%
Other

Memgraph

AI Development · Databases

4,628

High-performance in-memory graph database for AI context and real-time analytics

View details
90
Repo Health
79
Technical
66
Dependency
Built with
C++ 66%
Python 18%
Updated 6 days ago
Python
66%
Other

Morphik

AI Development · Databases · Search

3,717

Morphik is an AI-native ingestion and retrieval engine that lets developers store, search, and reason over visually rich documents — scanned PDFs, manuals, slides, and video — without duct-taping together OCR, an embedding model, and a vector database.

View details
64
Repo Health
71
Technical
67
Dependency
Built with
Python 66%
TypeScript 23%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers