lxml

Parsing

Análise de XML e HTML

Visão geral lxml

lxml é a biblioteca Python com mais recursos para processamento de XML e HTML. Ele combina o melhor de libxml2 e libxslt com uma API Pythonic, oferecendo análise extremamente rápida, suporte completo a XPath 1.0, transformações XSLT e manipulação confiável até mesmo de documentos malformados.

PyRuninclui lxml por meio da distribuição do pacotePyodide. Você pode analisar strings XML, executar consultas XPath e navegar em árvores de documentos inteiramente em seu navegador — não é necessária instalação local de libxml2 ou qualquer outra biblioteca C.

Saída de código e execução

Analise XML e execute consultas XPath.

lxml XML ParserExecutar no 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)

Pacotes Relacionados

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.