tinyhtml5 is a HTML5 parser, currently at version 2.1.0, that transforms a possibly malformed HTML document into an ElementTree tree. It is a simplified and modernized fork of the unmaintained `html5lib` library, focusing solely on parsing and generating `ElementTree` output. It typically releases updates to support new Python versions and minor feature enhancements.
pip install tinyhtml5Verified import paths — ran on the pinned version, not inferred.
Parses an HTML string into an ElementTree object.
Upgrade to Python 3.10+ or pin tinyhtml5 to '<2.1.0' if Python 3.9 is required.
Carefully review the 'Going Further' documentation, especially the 'What are the differences with html5lib?' section, if migrating from `html5lib`. Adapt code to work solely with `ElementTree` for tree manipulation.
Familiarize yourself with the `xml.etree.ElementTree` API for navigating and manipulating the parsed HTML document, or explicitly convert the `ElementTree` to your preferred format/library.
Always check if an `ElementTree` element has children (e.g., `if len(element) > index:`) before direct indexing, or use `element.find('tag')` for single child lookups or `element.findall('tag')` for multiple, which return `None` or an empty list respectively when no matches are found, avoiding `IndexError`.Install the library using pip: `pip install tinyhtml5`
tinyhtml5 does not provide 'treewalkers'. If you need to traverse the ElementTree generated by tinyhtml5, use standard ElementTree methods or process the output with another library.
Import the `html5parser` submodule and use its `parse` or `fromstring` functions: `from tinyhtml5 import html5parser` then `html5parser.parse(...)`.
Remove the `treebuilder` argument, as tinyhtml5 defaults to ElementTree output: `from tinyhtml5 import html5parser` then `html5parser.parse(source)`.