| 1 |
html5lib |
| 2 |
======== |
| 3 |
|
| 4 |
.. image:: https://github.com/html5lib/html5lib-python/actions/workflows/python-tox.yml/badge.svg |
| 5 |
:target: https://github.com/html5lib/html5lib-python/actions/workflows/python-tox.yml |
| 6 |
|
| 7 |
html5lib is a pure-python library for parsing HTML. It is designed to |
| 8 |
conform to the WHATWG HTML specification, as is implemented by all major |
| 9 |
web browsers. |
| 10 |
|
| 11 |
|
| 12 |
Usage |
| 13 |
----- |
| 14 |
|
| 15 |
Simple usage follows this pattern: |
| 16 |
|
| 17 |
.. code-block:: python |
| 18 |
|
| 19 |
import html5lib |
| 20 |
with open("mydocument.html", "rb") as f: |
| 21 |
document = html5lib.parse(f) |
| 22 |
|
| 23 |
or: |
| 24 |
|
| 25 |
.. code-block:: python |
| 26 |
|
| 27 |
import html5lib |
| 28 |
document = html5lib.parse("<p>Hello World!") |
| 29 |
|
| 30 |
By default, the ``document`` will be an ``xml.etree`` element instance. |
| 31 |
Whenever possible, html5lib chooses the accelerated ``ElementTree`` |
| 32 |
implementation. |
| 33 |
|
| 34 |
Two other tree types are supported: ``xml.dom.minidom`` and |
| 35 |
``lxml.etree``. To use an alternative format, specify the name of |
| 36 |
a treebuilder: |
| 37 |
|
| 38 |
.. code-block:: python |
| 39 |
|
| 40 |
import html5lib |
| 41 |
with open("mydocument.html", "rb") as f: |
| 42 |
lxml_etree_document = html5lib.parse(f, treebuilder="lxml") |
| 43 |
|
| 44 |
When using with ``urllib.request`` (Python 3), the charset from HTTP |
| 45 |
should be pass into html5lib as follows: |
| 46 |
|
| 47 |
.. code-block:: python |
| 48 |
|
| 49 |
from urllib.request import urlopen |
| 50 |
import html5lib |
| 51 |
|
| 52 |
with urlopen("http://example.com/") as f: |
| 53 |
document = html5lib.parse(f, transport_encoding=f.info().get_content_charset()) |
| 54 |
|
| 55 |
To have more control over the parser, create a parser object explicitly. |
| 56 |
For instance, to make the parser raise exceptions on parse errors, use: |
| 57 |
|
| 58 |
.. code-block:: python |
| 59 |
|
| 60 |
import html5lib |
| 61 |
with open("mydocument.html", "rb") as f: |
| 62 |
parser = html5lib.HTMLParser(strict=True) |
| 63 |
document = parser.parse(f) |
| 64 |
|
| 65 |
When you're instantiating parser objects explicitly, pass a treebuilder |
| 66 |
class as the ``tree`` keyword argument to use an alternative document |
| 67 |
format: |
| 68 |
|
| 69 |
.. code-block:: python |
| 70 |
|
| 71 |
import html5lib |
| 72 |
parser = html5lib.HTMLParser(tree=html5lib.getTreeBuilder("dom")) |
| 73 |
minidom_document = parser.parse("<p>Hello World!") |
| 74 |
|
| 75 |
More documentation is available at https://html5lib.readthedocs.io/. |
| 76 |
|
| 77 |
|
| 78 |
Installation |
| 79 |
------------ |
| 80 |
|
| 81 |
html5lib works on CPython 3.8+ and PyPy. To install: |
| 82 |
|
| 83 |
.. code-block:: bash |
| 84 |
|
| 85 |
$ pip install html5lib |
| 86 |
|
| 87 |
The goal is to support a (non-strict) superset of the versions that `pip |
| 88 |
supports |
| 89 |
<https://pip.pypa.io/en/stable/installing/#python-and-os-compatibility>`_. |
| 90 |
|
| 91 |
Optional Dependencies |
| 92 |
--------------------- |
| 93 |
|
| 94 |
The following third-party libraries may be used for additional |
| 95 |
functionality: |
| 96 |
|
| 97 |
- ``lxml`` is supported as a tree format (for both building and |
| 98 |
walking) under CPython (but *not* PyPy where it is known to cause |
| 99 |
segfaults); |
| 100 |
|
| 101 |
- ``genshi`` has a treewalker (but not builder); and |
| 102 |
|
| 103 |
- ``chardet`` can be used as a fallback when character encoding cannot |
| 104 |
be determined. |
| 105 |
|
| 106 |
|
| 107 |
Bugs |
| 108 |
---- |
| 109 |
|
| 110 |
Please report any bugs on the `issue tracker |
| 111 |
<https://github.com/html5lib/html5lib-python/issues>`_. |
| 112 |
|
| 113 |
|
| 114 |
Tests |
| 115 |
----- |
| 116 |
|
| 117 |
Unit tests require the ``pytest`` and ``mock`` libraries and can be |
| 118 |
run using the ``pytest`` command in the root directory. |
| 119 |
|
| 120 |
Test data are contained in a separate `html5lib-tests |
| 121 |
<https://github.com/html5lib/html5lib-tests>`_ repository and included |
| 122 |
as a submodule, thus for git checkouts they must be initialized:: |
| 123 |
|
| 124 |
$ git submodule init |
| 125 |
$ git submodule update |
| 126 |
|
| 127 |
If you have all compatible Python implementations available on your |
| 128 |
system, you can run tests on all of them using the ``tox`` utility, |
| 129 |
which can be found on PyPI. |
| 130 |
|
| 131 |
|
| 132 |
Questions? |
| 133 |
---------- |
| 134 |
|
| 135 |
Check out `the docs <https://html5lib.readthedocs.io/en/latest/>`_. Still |
| 136 |
need help? Go to our `GitHub Discussions |
| 137 |
<https://github.com/html5lib/html5lib-python/discussions>`_. |
| 138 |
|
| 139 |
You can also browse the archives of the `html5lib-discuss mailing list |
| 140 |
<https://www.mail-archive.com/[email protected]/>`_. |