html5libvsuntangle
html5lib is a pure-python library for parsing HTML. It is designed to conform to the WHATWG HTML specification, as is implemented by all major web browsers.
As html5lib is implemented in pure-python it is significantly slower than alternatives powered by lxml
(like parsel
or beautifulsoup
).
However, html5lib implements a more true html5 parsing which can represent HTML tree more correctly than alternatives.
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 the
untangle.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)
Example Use
import html5lib
from html5lib import parse
html_doc = "<html><head><title>My Title</title></head><body></body></html>"
parsed = parse(html_doc)
title = parsed.getElementsByTagName("title")[0]
print(title.childNodes[0].nodeValue)
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")