Skip to content

lxmlvsuntangle

NOASSERTION 19 13 2,614
80.4 million (month) Dec 13 2022 5.2.2(2 months ago)
609 2 24 MIT
Jun 09 2011 142.0 thousand (month) 1.2.1(2 years ago)

lxml is a low-level XML and HTML tree processor. It's used by many other libraries such as parsel or beautifulsoup for higher level HTML parsing.

One of the main features of lxml is its speed and efficiency.
It is built on top of the libxml2 and libxslt C libraries, which are known for their high performance and low memory footprint. This makes lxml well-suited for processing large and complex XML and HTML documents.

One of the key components of lxml is the ElementTree API, which is modeled after the ElementTree API from the Python standard library's xml module. This API provides a simple and intuitive way to access and manipulate the elements and attributes of an XML or HTML document. It also provides a powerful and flexible Xpath engine that allows you to select elements based on their names, attributes, and contents.

Another feature of lxml is its support for parsing and creating XML documents using the XSLT standard. The lxml library provides a powerful and easy-to-use interface for applying XSLT stylesheets to XML documents, which can be used to transform and convert XML documents into other formats, such as HTML, PDF, or even other XML formats.

For web scraping it's best to use other higher level libraries that use lxml like parsel or beautifulsoup

untangle is a simple library for parsing XML documents in Python. It allows you to access data in an XML file as if it were a Python object, making it easy to work with the data in your code.

To use untangle, you first need to install it via pip by running pip install untangle``. Once it is installed, you can use theuntangle.parse()`` function to parse an XML file and create a Python object.

For example:

import untangle

obj = untangle.parse("example.xml")
print(obj.root.element.child)

You can also pass a file-like object or a string containing XML data to the untangle.parse() function. Once you have an untangle object, you can access elements in the XML document using dot notation.

You can also access the attributes of an element by using attrib property, eg. `obj.root.element['attrib_name']`` untangle also supports xpath-like syntax to access the elements, obj.root.xpath("path/to/element")

It also supports iteration over the elements using obj.root.element.children

for child in obj.root.element.children:
    print(child)

Highlights


low-levelfast

Example Use


from lxml import etree

# this is our HTML page:
html = """
<head>
  <title>Hello World!</title>
</head>
<body>
  <div id="product">
    <h1>Product Title</h1>
    <p>paragraph 1</p>
    <p>paragraph2</p>
    <span class="price">$10</span>
  </div>
</body>
"""

tree = tree.fromstring(html)

# for parsing, LXML only supports XPath selectors:
tree.xpath('//span[@class="price"]')[0].text
"$10"
import untangle

obj = untangle.parse("example.xml")

print(obj.root.element.child)
# access attributes:
print(obj.root.element['attrib_name'])
# use xpath:
element = obj.root.xpath("path/to/element")

Alternatives / Similar


Was this page helpful?