Skip to content

untanglevspyquery

MIT 21 2 632
442.1 thousand (month) Jun 09 2011 1.2.1(2022-07-02 14:09:28 ago)
2,381 5 55 NOASSERTION
Dec 05 2008 2.0 million (month) 2.0.1(2024-08-30 08:12:22 ago)

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 python for child in obj.root.element.children: print(child)

PyQuery is a Python library for working with XML and HTML documents. It is similar to BeautifulSoup and is often used as a drop-in replacement for it.

PyQuery is inspired by javascript's jQuery and uses similar API allowing selecting of HTML nodes through CSS selectors. This makes it easy for developers who are already familiar with jQuery to use PyQuery in Python.

Unlike jQuery, PyQuery doesn't support XPath selectors and relies entirely on CSS selectors though offers similar HTML parsing features like selection of HTML elements, their attributes and text as well as html tree modification.

PyQuery also comes with a http client (through requests) so it can load and parse web URLs by itself.

Highlights


css-selectors

Example Use


```python 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") ```
```python from pyquery import PyQuery as pq # this is our HTML page: html = """ Hello World!

Product Title

paragraph 1

paragraph2

$10
""" doc = pq(html) # we can use CSS selectors: print(doc('#product .price').text()) "$10" # it's also possible to modify HTML tree in various ways: # insert text into selected element: print(doc('h1').append('discounted')) "

Product Titlediscounted

" # or remove elements doc('p').remove() print(doc('#product').html()) """

Product Titlediscounted

$10 """ # pyquery can also retrieve web documents using requests: doc = pq(url='http://httpbin.org/html', headers={"User-Agent": "webscraping.fyi"}) print(doc('h1').html()) ```

Alternatives / Similar


Was this page helpful?