lxml
ParsingXML & HTML parsing
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.
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
Related Packages
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.