| 1 |
=============== |
| 2 |
html5lib Parser |
| 3 |
=============== |
| 4 |
|
| 5 |
`html5lib`_ is a Python package that implements the HTML5 parsing algorithm |
| 6 |
which is heavily influenced by current browsers and based on the `WHATWG |
| 7 |
HTML5 specification`_. |
| 8 |
|
| 9 |
.. _html5lib: http://code.google.com/p/html5lib/ |
| 10 |
.. _BeautifulSoup: http://www.crummy.com/software/BeautifulSoup/ |
| 11 |
.. _WHATWG HTML5 specification: http://www.whatwg.org/specs/web-apps/current-work/ |
| 12 |
|
| 13 |
lxml can benefit from the parsing capabilities of `html5lib` through |
| 14 |
the ``lxml.html.html5parser`` module. It provides a similar interface |
| 15 |
to the ``lxml.html`` module by providing ``fromstring()``, |
| 16 |
``parse()``, ``document_fromstring()``, ``fragment_fromstring()`` and |
| 17 |
``fragments_fromstring()`` that work like the regular html parsing |
| 18 |
functions. |
| 19 |
|
| 20 |
|
| 21 |
Differences to regular HTML parsing |
| 22 |
=================================== |
| 23 |
|
| 24 |
There are a few differences in the returned tree to the regular HTML |
| 25 |
parsing functions from ``lxml.html``. html5lib normalizes some elements |
| 26 |
and element structures to a common format. For example, even if a table |
| 27 |
does not have a `tbody` html5lib will inject one automatically: |
| 28 |
|
| 29 |
.. sourcecode:: pycon |
| 30 |
|
| 31 |
>>> from lxml.html import tostring, html5parser |
| 32 |
>>> tostring(html5parser.fromstring("<table><td>foo")) |
| 33 |
'<table><tbody><tr><td>foo</td></tr></tbody></table>' |
| 34 |
|
| 35 |
Also the parameters the functions accept are different. |
| 36 |
|
| 37 |
|
| 38 |
Function Reference |
| 39 |
================== |
| 40 |
|
| 41 |
``parse(filename_url_or_file)``: |
| 42 |
Parses the named file or url, or if the object has a ``.read()`` |
| 43 |
method, parses from that. |
| 44 |
|
| 45 |
``document_fromstring(html, guess_charset=True)``: |
| 46 |
Parses a document from the given string. This always creates a |
| 47 |
correct HTML document, which means the parent node is ``<html>``, |
| 48 |
and there is a body and possibly a head. |
| 49 |
|
| 50 |
If a bytestring is passed and ``guess_charset`` is true the chardet |
| 51 |
library (if installed) will guess the charset if ambiguities exist. |
| 52 |
|
| 53 |
``fragment_fromstring(string, create_parent=False, guess_charset=False)``: |
| 54 |
Returns an HTML fragment from a string. The fragment must contain |
| 55 |
just a single element, unless ``create_parent`` is given; |
| 56 |
e.g., ``fragment_fromstring(string, create_parent='div')`` will |
| 57 |
wrap the element in a ``<div>``. If ``create_parent`` is true the |
| 58 |
default parent tag (div) is used. |
| 59 |
|
| 60 |
If a bytestring is passed and ``guess_charset`` is true the chardet |
| 61 |
library (if installed) will guess the charset if ambiguities exist. |
| 62 |
|
| 63 |
``fragments_fromstring(string, no_leading_text=False, parser=None)``: |
| 64 |
Returns a list of the elements found in the fragment. The first item in |
| 65 |
the list may be a string. If ``no_leading_text`` is true, then it will |
| 66 |
be an error if there is leading text, and it will always be a list of |
| 67 |
only elements. |
| 68 |
|
| 69 |
If a bytestring is passed and ``guess_charset`` is true the chardet |
| 70 |
library (if installed) will guess the charset if ambiguities exist. |
| 71 |
|
| 72 |
``fromstring(string)``: |
| 73 |
Returns ``document_fromstring`` or ``fragment_fromstring``, based |
| 74 |
on whether the string looks like a full document, or just a |
| 75 |
fragment. |
| 76 |
|
| 77 |
Additionally all parsing functions accept a ``parser`` keyword argument |
| 78 |
that can be set to a custom parser instance. To create custom parsers |
| 79 |
you can subclass the ``HTMLParser`` and ``XHTMLParser`` from the same |
| 80 |
module. Note that these are the parser classes provided by html5lib. |