html5rdf is a pure-python library for parsing HTML to DOMFragment objects, primarily intended for use within RDFLib. It is a fork of `html5lib-python` and `html5lib-modern`, designed to conform to the WHATWG HTML specification. Maintained by the RDFLib team, it serves as a drop-in replacement for `html5lib` without Python 2 support or legacy dependencies like `six` and `webencodings`. The current version is 1.2.1, with releases occurring as needed for bug fixes and RDFLib integration.
pip install html5rdfVerified import paths — ran on the pinned version, not inferred.
This example demonstrates basic HTML parsing from both a string and a file-like object using `html5rdf.parse`. By default, it returns an `xml.etree` element instance. You can specify different treebuilders like 'lxml' or 'dom' (for `xml.dom.minidom`) during parsing.
Ensure only `html5rdf` or a compatible `html5lib` version is in your project's dependencies, but not both at the same time if aliasing occurs.
Avoid using the `lxml` treebuilder when running Python applications with PyPy. Opt for `xml.etree` (default) or `xml.dom.minidom` instead.
For HTML sanitization needs, migrate to dedicated libraries like `Bleach`, which is recommended by the original `html5lib` project. `html5rdf` focuses solely on parsing HTML to DOM fragments.
To prevent these exceptions, initialize the parser with `strict=False` (default behavior): `parser = html5rdf.HTMLParser(strict=False)`. Alternatively, handle `html5lib.html5parser.ParseError` exceptions in your code if strict parsing is desired.
This is likely a packaging or test-specific issue that does not necessarily reflect on the core parsing functionality. If encountering this, monitor the GitHub issues for a fix or consider checking out the repository and trying development branch if available. For normal usage, parsing functionality should still be stable.
Ensure that the HTML fragments being parsed are as semantically complete as possible or be aware of `html5rdf`'s behavior with incomplete/invalid fragments. The bug might require upstream fixes in the parsing logic for fragments. Verify the output structure after parsing if unexpected results occur.