Registry / serialization / html5rdf

html5rdf

JSON →
library1.2.1pypypi✓ verified 84d ago

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 html5rdf
INSTALL
IMPORT
SIG · HTML5RDF
H
html5rdf
serializationpythonv1.2.1
Install
1.6s avg
Import
204ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.215s · 18.9MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.194s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

html5rdf
import html5rdf
HTMLParser
from html5rdf import HTMLParser
getTreeBuilder
from html5rdf import getTreeBuilder
import html5rdf.getTreeBuilder
getTreeBuilder is typically accessed directly from the top-level html5rdf module or via a parser instance.

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.

import html5rdf # Parse a string document_from_string = html5rdf.parse("<p>Hello World!</p>") print(f"Parsed from string: {document_from_string.tag}") # Parse from a file-like object html_content = b"<html><body><h1>Test</h1></body></html>" import io with io.BytesIO(html_content) as f: document_from_file = html5rdf.parse(f) print(f"Parsed from file: {document_from_file.tag}")
Debug
Known issues
breakingDo not install `html5rdf` alongside older `html5lib` or `html5lib-modern` packages. `html5rdf` is a fork and exposes the module under the same name internally, leading to aliasing issues and unexpected behavior if both are present in the dependency tree.
fix
Ensure only `html5rdf` or a compatible `html5lib` version is in your project's dependencies, but not both at the same time if aliasing occurs.
affects: All versions of html5rdf (when co-installed with aliasing html5lib versions)
gotchaWhen using the `lxml` treebuilder (e.g., `html5rdf.parse(html, treebuilder='lxml')`), `lxml` is supported under CPython but is known to cause segfaults when used with PyPy.
fix
Avoid using the `lxml` treebuilder when running Python applications with PyPy. Opt for `xml.etree` (default) or `xml.dom.minidom` instead.
affects: All versions
deprecatedThe `html5lib` sanitizer functionality (e.g., `html5lib.serialize(sanitize=True)` or `html5lib.filters.sanitizer`) has been removed from `html5rdf` as it was deprecated in the upstream `html5lib` project.
fix
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.
affects: All versions
Errors
Common errors & fixes
html5lib.html5parser.ParseError: Unexpected DOCTYPE. Ignored.
`html5rdf` (inheriting from `html5lib`) can raise `ParseError` exceptions when parsing HTML documents that contain unexpected or malformed DOCTYPE declarations, or other parsing errors when the parser is initialized with `strict=True`.
fix
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.
Tests are failing in html5rdf 1.2.1 after installation.
A known issue in version 1.2.1 exists where included unit tests may fail due to specific changes or test data packaging.
fix
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.
Casting an HTML literal with certain content to `rdf:HTML` datatype leads to an empty literal or incorrect output when used with RDFLib.
This issue arises from `html5rdf`'s underlying `html5lib` parsing of HTML fragments, particularly those that are not valid standalone documents (e.g., `<body>` or `<tr>` without proper parent elements). It may result in fragments with no children or incorrect child nodes.
fix
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.
Upgrade
Version history
1.2.1latest on PyPI · released Oct 30, 2024
Audit
Dependencies
lxmloptionalOptional, for accelerated ElementTree implementation and `lxml.etree` treebuilder.
genshioptionalOptional, for a treewalker.
chardetoptionalOptional, as a fallback for character encoding detection.
Agent activity
4 hits · last 30 days
node
4
Resources
html5rdf — pip install html5rdf · libregistry