lxml

Parsing

XML & HTML parsing

Try lxml in PyRun

What is lxml?

lxml is the most feature-complete Python library for processing XML and HTML. It combines the best of libxml2 and libxslt with a Pythonic API, offering extremely fast parsing, full XPath 1.0 support, XSLT transformations, and reliable handling of even malformed documents.

PyRun includes lxml via the Pyodide package distribution. You can parse XML strings, run XPath queries, and navigate document trees entirely in your browser — no local installation of libxml2 or any other C library required.

Code Example

Parse XML and run XPath queries.

lxml XML Parser
Try in Editor
from lxml import etree

xml_data = """
<bookstore>
  <book category="science">
    <title>A Brief History of Time</title>
    <author>Stephen Hawking</author>
    <price>12.99</price>
  </book>
  <book category="fiction">
    <title>1984</title>
    <author>George Orwell</author>
    <price>9.99</price>
  </book>
</bookstore>
"""

root = etree.fromstring(xml_data.strip())

print("All books:")
for book in root.findall("book"):
    title  = book.find("title").text
    author = book.find("author").text
    price  = float(book.find("price").text)
    print(f"  {title} by {author} — ${price:.2f}")

expensive = root.xpath("//book[price > 10]/title/text()")
print("\nBooks over $10:", expensive)

Why run lxml in PyRun?

  • Zero setup — no pip install, no virtual environment, no Python download
  • Instant results — powered by WebAssembly, runs locally in your browser
  • Share your code — generate a link and anyone can run it instantly
  • Works offline — after first load, PyRun runs without internet
Open editor with lxml example

Related Packages