lxml - XML and HTML with Python

Stephan Richter

lxml is the most feature-rich and easy-to-use library for processing XML and HTML in the Python language.

Introduction

The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt. It is unique in that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API, mostly compatible but superior to the well-known ElementTree API. The latest release works with all CPython versions from 2.7 to 3.9. See the introduction for more information about background and goals of the lxml project. Some common questions are answered in the FAQ.

Support the project

lxml has been downloaded from the Python Package Index millions of times and is also available directly in many package distributions, e.g. for Linux or macOS.

Most people who use lxml do so because they like using it. You can show us that you like it by blogging about your experience with it and linking to the project website.

If you are using lxml for your work and feel like giving a bit of your own benefit back to support the project, consider sending us money through GitHub Sponsors, Tidelift or PayPal that we can use to buy us free time for the maintenance of this great library, to fix bugs in the software, review and integrate code contributions, to improve its features and documentation, or to just take a deep breath and have a cup of tea every once in a while. Please read the Legal Notice below, at the bottom of this page. Thank you for your support.

Support lxml through GitHub Sponsors

via a Tidelift subscription

or via PayPal:

Donate to the lxml project

Please contact Stefan Behnel for other ways to support the lxml project, as well as commercial consulting, customisations and trainings on lxml and fast Python XML processing.

Note that we are not accepting donations in crypto currencies. Much of the development and hosting for lxml is done in a carbon-neutral way or with compensated and very low emissions. Crypto currencies do not fit into that ambition.

AppVeyor and GitHub Actions support the lxml project with their build and CI servers. Jetbrains supports the lxml project by donating free licenses of their PyCharm IDE. Another supporter of the lxml project is COLOGNE Webdesign.

Documentation

The HTML documentation from this web site is part of the normal source download.

lxml.etree follows the ElementTree API as much as possible, building it on top of the native libxml2 tree. If you are new to ElementTree, start with the lxml.etree tutorial for XML processing. See also the ElementTree compatibility overview and the ElementTree performance page comparing lxml to the original ElementTree and cElementTree implementations.

Right after the lxml.etree tutorial for XML processing and the ElementTree documentation, the next place to look is the lxml.etree specific API documentation. It describes how lxml extends the ElementTree API to expose libxml2 and libxslt specific XML functionality, such as XPath, Relax NG, XML Schema, XSLT, and c14n (including c14n 2.0). Python code can be called from XPath expressions and XSLT stylesheets through the use of XPath extension functions. lxml also offers a SAX compliant API, that works with the SAX support in the standard library.

There is a separate module lxml.objectify that implements a data-binding API on top of lxml.etree. See the objectify and etree FAQ entry for a comparison.

In addition to the ElementTree API, lxml also features a sophisticated API for custom XML element classes. This is a simple way to write arbitrary XML driven APIs on top of lxml. lxml.etree also has a C-level API that can be used to efficiently extend lxml.etree in external C modules, including fast custom element class support.

Download

The best way to download lxml is to visit lxml at the Python Package Index (PyPI). It has the source that compiles on various platforms. The source distribution is signed with this key.

The latest version is lxml 4.9.2, released 2022-12-13 (changes for 4.9.2). Older versions are listed below.

Please take a look at the installation instructions !

This complete website (including the generated API documentation) is part of the source distribution, so if you want to download the documentation for offline use, take the source archive and copy the doc/html directory out of the source tree.

The latest installable developer sources are available from Github. It's also possible to check out the latest development version of lxml from Github directly, using a command like this:

git clone https://github.com/lxml/lxml.git lxml

You can browse the source repository and its history through the web. Please read how to build lxml from source first. The latest CHANGES of the developer version are also accessible. You can check there if a bug you found has been fixed or a feature you want has been implemented in the latest trunk version.

Mailing list

Questions? Suggestions? Code to contribute? We have a mailing list.

You can also search the archive for past questions and discussions.

Bug tracker

lxml uses the launchpad bug tracker. If you are sure you found a bug in lxml, please file a bug report there. If you are not sure whether some unexpected behaviour of lxml is a bug or not, please check the documentation and ask on the mailing list first. Do not forget to search the archive!

License

The lxml library is shipped under a BSD license. libxml2 and libxslt2 itself are shipped under the MIT license. There should therefore be no obstacle to using lxml in your codebase.

Old Versions

See the websites of lxml 4.8, 4.7, 4.6, 4.5, 4.4, 4.3, 4.2, 4.1, 4.0, 3.8, 3.7, 3.6, 3.5, 3.4, 3.3, 3.2, 3.1, 3.0, 2.3, 2.2, 2.1, 2.0, 1.3

Project income report

lxml has more than 50 million downloads per month on PyPI.