html.html5parser

An interface to html5lib that mimics the lxml.html interface.

Module Contents

Classes

HTMLParser(self,strict=False,**kwargs) An html5lib HTML parser with lxml as tree.
XHTMLParser(self,strict=False,**kwargs) An html5lib XHTML Parser with lxml as tree.

Functions

_find_tag(tree,tag)
document_fromstring(html,guess_charset=None,parser=None) Parse a whole document into a string.
fragments_fromstring(html,no_leading_text=False,guess_charset=None,parser=None) Parses several HTML elements, returning a list of elements.
fragment_fromstring(html,create_parent=False,guess_charset=None,parser=None) Parses a single HTML element; it is an error if there is more than
fromstring(html,guess_charset=None,parser=None) Parse the html, returning a single element/document.
parse(filename_url_or_file,guess_charset=None,parser=None) Parse a filename, URL, or file-like object into an HTML document
_looks_like_url(str)
class HTMLParser(strict=False, **kwargs)

An html5lib HTML parser with lxml as tree.

__init__(strict=False, **kwargs)
class XHTMLParser(strict=False, **kwargs)

An html5lib XHTML Parser with lxml as tree.

__init__(strict=False, **kwargs)
_find_tag(tree, tag)
document_fromstring(html, guess_charset=None, parser=None)

Parse a whole document into a string.

If guess_charset is true, or if the input is not Unicode but a byte string, the chardet library will perform charset guessing on the string.

fragments_fromstring(html, no_leading_text=False, guess_charset=None, parser=None)

Parses several HTML elements, returning a list of elements.

The first item in the list may be a string. If no_leading_text is true, then it will be an error if there is leading text, and it will always be a list of only elements.

If guess_charset is true, the chardet library will perform charset guessing on the string.

fragment_fromstring(html, create_parent=False, guess_charset=None, parser=None)

Parses a single HTML element; it is an error if there is more than one element, or if anything but whitespace precedes or follows the element.

If ‘create_parent’ is true (or is a tag name) then a parent node will be created to encapsulate the HTML in a single element. In this case, leading or trailing text is allowed.

If guess_charset is true, the chardet library will perform charset guessing on the string.

fromstring(html, guess_charset=None, parser=None)

Parse the html, returning a single element/document.

This tries to minimally parse the chunk of text, without knowing if it is a fragment or a document.

‘base_url’ will set the document’s base_url attribute (and the tree’s docinfo.URL)

If guess_charset is true, or if the input is not Unicode but a byte string, the chardet library will perform charset guessing on the string.

parse(filename_url_or_file, guess_charset=None, parser=None)

Parse a filename, URL, or file-like object into an HTML document tree. Note: this returns a tree, not an element. Use parse(...).getroot() to get the document root.

If guess_charset is true, the useChardet option is passed into html5lib to enable character detection. This option is on by default when parsing from URLs, off by default when parsing from file(-like) objects (which tend to return Unicode more often than not), and on by default when parsing from a file path (which is read in binary mode).

_looks_like_url(str)