GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • lxml
  • Why lxml?
  • Installing lxml
  • Benchmarks and Speed

This view

  • Original page
  • New address

GeezerVison is showing lxml.de — html5lib Parser

Menu
Like the tool?
Help making it better!
Your donation helps!
  • lxml
    • lxml
      • Introduction
      • Support the project
      • Documentation
      • Download
      • Mailing list
      • Bug tracker
      • License
      • Old Versions
      • Project income report
      • Legal Notice for Donations
    • Why lxml?
      • Motto
      • Aims
    • Installing lxml
      • Where to get it
      • Requirements
      • Installation
      • Building lxml from dev sources
      • Using lxml with python-libxml2
      • Source builds on MS Windows
      • Source builds on MacOS-X
    • Benchmarks and Speed
      • General notes
      • How to read the timings
      • Parsing and Serialising
      • The ElementTree API
      • XPath
      • A longer example
      • lxml.objectify
    • ElementTree compatibility of lxml.etree
    • lxml FAQ - Frequently Asked Questions
      • General Questions
      • Installation
      • Contributing
      • Bugs
      • Threading
      • Parsing and Serialisation
      • XPath and Document Traversal
  • Developing with lxml
    • The lxml.etree Tutorial
      • The Element class
      • The ElementTree class
      • Parsing from strings and files
      • Namespaces
      • The E-factory
      • ElementPath
    • API reference
    • APIs specific to lxml.etree
      • lxml.etree
      • Other Element APIs
      • Trees and Documents
      • Iteration
      • Error handling on exceptions
      • Error logging
      • Serialisation
      • Incremental XML generation
      • CDATA
      • XInclude and ElementInclude
    • Parsing XML and HTML with lxml
      • Parsers
      • The target parser interface
      • The feed parser interface
      • Incremental event parsing
      • iterparse and iterwalk
      • Python unicode strings
    • Validation with lxml
      • Validation at parse time
      • DTD
      • RelaxNG
      • XMLSchema
      • Schematron
      • (Pre-ISO-Schematron)
    • XPath and XSLT with lxml
      • XPath
      • XSLT
    • lxml.objectify
      • The lxml.objectify API
      • Asserting a Schema
      • ObjectPath
      • Python data types
      • How data types are matched
      • What is different from lxml.etree?
    • lxml.html
      • Parsing HTML
      • HTML Element Methods
      • Running HTML doctests
      • Creating HTML with the E-factory
      • Working with links
      • Forms
      • HTML Diff
      • Examples
    • lxml.cssselect
      • The CSSSelector class
      • The cssselect method
      • Supported Selectors
      • Namespaces
    • BeautifulSoup Parser
      • Parsing with the soupparser
      • Entity handling
      • Using soupparser as a fallback
      • Using only the encoding detection
    • html5lib Parser
      • Differences to regular HTML parsing
      • Function Reference
  • Extending lxml
    • Document loading and URL resolving
      • XML Catalogs
      • URI Resolvers
      • Document loading in context
      • I/O access control in XSLT
    • Python extensions for XPath and XSLT
      • XPath Extension functions
      • XSLT extension elements
    • Using custom Element classes in lxml
      • Background on Element proxies
      • Element initialization
      • Setting up a class lookup scheme
      • Generating XML with custom classes
      • Implementing namespaces
    • Sax support
      • Building a tree from SAX events
      • Producing SAX events from an ElementTree or Element
      • Interfacing with pulldom/minidom
    • The public C-API of lxml.etree
      • Passing generated trees through Python
      • Writing external modules in Cython
      • Writing external modules in C
  • Developing lxml
    • How to build lxml from source
      • Cython
      • Github, git and hg
      • Building the sources
      • Running the tests and reporting errors
      • Building an egg or wheel
      • Building lxml on MacOS-X
      • Static linking on Windows
      • Building Debian packages from SVN sources
    • How to read the source of lxml
      • What is Cython?
      • Where to start?
      • lxml.etree
      • Python modules
      • lxml.objectify
      • lxml.html
    • Release Changelog
    • Credits
      • Main contributors
      • Special thanks goes to:
  • Sitemap
Like the tool?
Help making it better!
Your donation helps!

html5lib Parser

html5lib is a Python package that implements the HTML5 parsing algorithm which is heavily influenced by current browsers and based on the WHATWG HTML5 specification.

lxml can benefit from the parsing capabilities of html5lib through the lxml.html.html5parser module. It provides a similar interface to the lxml.html module by providing fromstring(), parse(), document_fromstring(), fragment_fromstring() and fragments_fromstring() that work like the regular html parsing functions.

Differences to regular HTML parsing

There are a few differences in the returned tree to the regular HTML parsing functions from lxml.html. html5lib normalizes some elements and element structures to a common format. For example, even if a table does not have a tbody html5lib will inject one automatically:

>>> from lxml.html import tostring, html5parser
>>> tostring(html5parser.fromstring("<table><td>foo"))
'<table><tbody><tr><td>foo</td></tr></tbody></table>'

Also the parameters the functions accept are different.

Function Reference

parse(filename_url_or_file):
Parses the named file or url, or if the object has a .read() method, parses from that.
document_fromstring(html, guess_charset=True):

Parses a document from the given string. This always creates a correct HTML document, which means the parent node is <html>, and there is a body and possibly a head.

If a bytestring is passed and guess_charset is true the chardet library (if installed) will guess the charset if ambiguities exist.

fragment_fromstring(string, create_parent=False, guess_charset=False):

Returns an HTML fragment from a string. The fragment must contain just a single element, unless create_parent is given; e.g., fragment_fromstring(string, create_parent='div') will wrap the element in a <div>. If create_parent is true the default parent tag (div) is used.

If a bytestring is passed and guess_charset is true the chardet library (if installed) will guess the charset if ambiguities exist.

fragments_fromstring(string, no_leading_text=False, parser=None):

Returns a list of the elements found in the fragment. 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 a bytestring is passed and guess_charset is true the chardet library (if installed) will guess the charset if ambiguities exist.

fromstring(string):
Returns document_fromstring or fragment_fromstring, based on whether the string looks like a full document, or just a fragment.

Additionally all parsing functions accept a parser keyword argument that can be set to a custom parser instance. To create custom parsers you can subclass the HTMLParser and XHTMLParser from the same module. Note that these are the parser classes provided by html5lib.


Generated on: 2026-06-16.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.