Skip to content
Go to file

Latest commit

Add a wrapper not to break pytest 4 (needed for Python 2 support).

    ============================= test session starts ==============================
    platform linux -- Python 3.9.0b5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
    rootdir: /builddir/build/BUILD/html5lib-1.1, configfile: pytest.ini
    plugins: expect-1.1.0
    collected 0 items / 1 error

    ==================================== ERRORS ====================================
    ________________________ ERROR collecting test session _________________________
    /usr/lib/python3.9/site-packages/pluggy/ in __call__
        return self._hookexec(self, self.get_hookimpls(), kwargs)
    /usr/lib/python3.9/site-packages/pluggy/ in _hookexec
        return self._inner_hookexec(hook, methods, kwargs)
    /usr/lib/python3.9/site-packages/pluggy/ in <lambda>
        self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
    html5lib/tests/ in pytest_collect_file
        return TokenizerFile(path, parent)
    /usr/lib/python3.9/site-packages/_pytest/ in __call__
        warnings.warn(NODE_USE_FROM_PARENT.format(name=self.__name__), stacklevel=2)
    E   pytest.PytestDeprecationWarning: Direct construction of TokenizerFile has been deprecated, please use TokenizerFile.from_parent.
    E   See for more details.

Fixes #505

Git stats


Failed to load latest commit information.
Latest commit message
Commit time
Apr 14, 2020
Apr 25, 2016
Apr 10, 2013



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.


Simple usage follows this pattern:

import html5lib
with open("mydocument.html", "rb") as f:
    document = html5lib.parse(f)


import html5lib
document = html5lib.parse("<p>Hello World!")

By default, the document will be an xml.etree element instance. Whenever possible, html5lib chooses the accelerated ElementTree implementation (i.e. xml.etree.cElementTree on Python 2.x).

Two other tree types are supported: xml.dom.minidom and lxml.etree. To use an alternative format, specify the name of a treebuilder:

import html5lib
with open("mydocument.html", "rb") as f:
    lxml_etree_document = html5lib.parse(f, treebuilder="lxml")

When using with urllib2 (Python 2), the charset from HTTP should be pass into html5lib as follows:

from contextlib import closing
from urllib2 import urlopen
import html5lib

with closing(urlopen("")) as f:
    document = html5lib.parse(f,"charset"))

When using with urllib.request (Python 3), the charset from HTTP should be pass into html5lib as follows:

from urllib.request import urlopen
import html5lib

with urlopen("") as f:
    document = html5lib.parse(f,

To have more control over the parser, create a parser object explicitly. For instance, to make the parser raise exceptions on parse errors, use:

import html5lib
with open("mydocument.html", "rb") as f:
    parser = html5lib.HTMLParser(strict=True)
    document = parser.parse(f)

When you're instantiating parser objects explicitly, pass a treebuilder class as the tree keyword argument to use an alternative document format:

import html5lib
parser = html5lib.HTMLParser(tree=html5lib.getTreeBuilder("dom"))
minidom_document = parser.parse("<p>Hello World!")

More documentation is available at


html5lib works on CPython 2.7+, CPython 3.5+ and PyPy. To install:

$ pip install html5lib

The goal is to support a (non-strict) superset of the versions that pip supports.

Optional Dependencies

The following third-party libraries may be used for additional functionality:

  • lxml is supported as a tree format (for both building and walking) under CPython (but not PyPy where it is known to cause segfaults);
  • genshi has a treewalker (but not builder); and
  • chardet can be used as a fallback when character encoding cannot be determined.


Please report any bugs on the issue tracker.


Unit tests require the pytest and mock libraries and can be run using the py.test command in the root directory.

Test data are contained in a separate html5lib-tests repository and included as a submodule, thus for git checkouts they must be initialized:

$ git submodule init
$ git submodule update

If you have all compatible Python implementations available on your system, you can run tests on all of them using the tox utility, which can be found on PyPI.


There's a mailing list available for support on Google Groups, html5lib-discuss, though you may get a quicker response asking on IRC in #whatwg on


Standards-compliant library for parsing and serializing HTML documents and fragments in Python




No packages published
You can’t perform that action at this time.