lxml
High-performance XML and HTML processing library combining libxml2/libxslt with Python.
3. Used by agents' projects
Dependency in 79 public GitHub projects set up for AI coding agents that we scanned (1.3%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What lxml does
lxml puts the C libraries libxml2 and libxslt behind a Python API modeled on ElementTree, and its README publishes a candid income report: well over 250 million downloads a month, EUR 3868.43 of project income in 2025.
lxml popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How lxml is used
- lxml is a dependency in 79 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (4.9% of the 1,623 Python ones), #4 of 141 in scraping, browsers and parsing.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 933; those are left out of every share on this page.
- The most used alternative in our scan is Beautiful Soup (133 projects).
- Projects that use it most often also use Beautiful Soup (70.9%), Requests (60.8%), python-dotenv (58.2%).
By coding agent
Often used with
lxml in practice
Strengths
The maintainers say lxml combines the speed and XML feature completeness of libxml2 and libxslt with a native Python API. That API is mostly compatible with ElementTree, so ElementTree tutorials largely carry over.
The docs cover validation at parse time and validation against DTD, RelaxNG, XML Schema and Schematron, all in the same library as XPath and XSLT.
You can call your own Python code from XPath expressions and XSLT stylesheets through extension functions.
Besides <code>lxml.html</code>, the docs describe a BeautifulSoup-backed <code>soupparser</code>. It can serve as a fallback or for encoding detection only. There is also an html5lib parser.
Watch out for
Standards compliance depends on libxml2 and libxslt. The FAQ says libxml2's XML Schema support is not complete.
The FAQ's list of implemented standards names XPath 1.0 and XSLT 1.0, plus EXSLT. Plan your expressions and stylesheets around those versions.
The FAQ admits there are places where lxml cannot match ElementTree behavior. These are listed in a separate compatibility document.
More about lxml
Funding
- GitHub Sponsors: https://github.com/scoder
- Tidelift: https://tidelift.com/funding/github/pypi/lxml
- 22 GitHub sponsors
Head to head
- Beautiful Soup vs lxml used together in 56 projects
Packages around it
- beautifulsoup4: HTML parsing
- requests: HTTP client
- python-dotenv: .env config loading
- pydantic: data validation
- numpy: numeric arrays
- aiohttp: async HTTP client/server
More in Scraping, browsers and parsing
Alternatives
lxml This page
Python HTML/XML parsing and screen-scraping library for web data extraction.
HTML parser based on WHATWG specification for parsing and processing HTML.
Get started
Install it.
pip install lxmlRules for your agent
## lxml
- Import the main module with `from lxml import etree`; the FAQ's examples assume it.
- Write against the ElementTree API first. Use lxml-only features (XPath, XSLT, Relax NG, XML Schema, c14n) when needed, knowing they tie the code to lxml.
- Keep XPath expressions and XSLT stylesheets to XPath 1.0 and XSLT 1.0, the versions in the FAQ's standards list.
- Do not assume complete XML Schema validation: the FAQ says libxml2's support "is currently not 100% complete".
- Before parsing untrusted input or exposing lxml in a web service, read the FAQ entries "Is lxml vulnerable to XML bombs?" and "How do I use lxml safely as a web-service endpoint?".
- Before sharing XSLT stylesheets across threads, read the FAQ's Threading section ("Why can't I reuse XSLT stylesheets in other threads?").
- Report bugs on Launchpad (bugs.launchpad.net/lxml), the issue tracker the README points to.