2
2
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
4
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
5
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
6
6
<title>Parsing XML and HTML with lxml</title>
7
7
<link rel="stylesheet" href="style.css" type="text/css" />
10
10
<div class="document" id="parsing-xml-and-html-with-lxml">
11
<div class="sidemenu"><ul id="lxml-section"><li><span class="section title">lxml</span><ul class="menu foreign" id="index-menu"><li class="menu title"><a href="index.html">lxml</a><ul class="submenu"><li class="menu item"><a href="index.html#introduction">Introduction</a></li><li class="menu item"><a href="index.html#documentation">Documentation</a></li><li class="menu item"><a href="index.html#download">Download</a></li><li class="menu item"><a href="index.html#mailing-list">Mailing list</a></li><li class="menu item"><a href="index.html#bug-tracker">Bug tracker</a></li><li class="menu item"><a href="index.html#license">License</a></li><li class="menu item"><a href="index.html#old-versions">Old Versions</a></li></ul></li></ul><ul class="menu foreign" id="intro-menu"><li class="menu title"><a href="intro.html">Why lxml?</a><ul class="submenu"><li class="menu item"><a href="intro.html#motto">Motto</a></li><li class="menu item"><a href="intro.html#aims">Aims</a></li></ul></li></ul><ul class="menu foreign" id="installation-menu"><li class="menu title"><a href="installation.html">Installing lxml</a><ul class="submenu"><li class="menu item"><a href="installation.html#requirements">Requirements</a></li><li class="menu item"><a href="installation.html#installation">Installation</a></li><li class="menu item"><a href="installation.html#building-lxml-from-sources">Building lxml from sources</a></li><li class="menu item"><a href="installation.html#ms-windows">MS Windows</a></li><li class="menu item"><a href="installation.html#macos-x">MacOS-X</a></li></ul></li></ul><ul class="menu foreign" id="lxml2-menu"><li class="menu title"><a href="lxml2.html">What's new in lxml 2.0?</a><ul class="submenu"><li class="menu item"><a href="lxml2.html#changes-in-etree-and-objectify">Changes in etree and objectify</a></li><li class="menu item"><a href="lxml2.html#new-modules">New modules</a></li></ul></li></ul><ul class="menu foreign" id="performance-menu"><li class="menu title"><a href="performance.html">Benchmarks and Speed</a><ul class="submenu"><li class="menu item"><a href="performance.html#general-notes">General notes</a></li><li class="menu item"><a href="performance.html#how-to-read-the-timings">How to read the timings</a></li><li class="menu item"><a href="performance.html#parsing-and-serialising">Parsing and Serialising</a></li><li class="menu item"><a href="performance.html#the-elementtree-api">The ElementTree API</a></li><li class="menu item"><a href="performance.html#xpath">XPath</a></li><li class="menu item"><a href="performance.html#a-longer-example">A longer example</a></li><li class="menu item"><a href="performance.html#lxml-objectify">lxml.objectify</a></li></ul></li></ul><ul class="menu foreign" id="compatibility-menu"><li class="menu title"><a href="compatibility.html">ElementTree compatibility of lxml.etree</a></li></ul><ul class="menu foreign" id="FAQ-menu"><li class="menu title"><a href="FAQ.html">lxml FAQ - Frequently Asked Questions</a><ul class="submenu"><li class="menu item"><a href="FAQ.html#general-questions">General Questions</a></li><li class="menu item"><a href="FAQ.html#installation">Installation</a></li><li class="menu item"><a href="FAQ.html#contributing">Contributing</a></li><li class="menu item"><a href="FAQ.html#bugs">Bugs</a></li><li class="menu item"><a href="FAQ.html#threading">Threading</a></li><li class="menu item"><a href="FAQ.html#parsing-and-serialisation">Parsing and Serialisation</a></li><li class="menu item"><a href="FAQ.html#xpath-and-document-traversal">XPath and Document Traversal</a></li></ul></li></ul></li></ul><ul id="Developing with lxml-section"><li><span class="section title">Developing with lxml</span><ul class="menu foreign" id="tutorial-menu"><li class="menu title"><a href="tutorial.html">The lxml.etree Tutorial</a><ul class="submenu"><li class="menu item"><a href="tutorial.html#the-element-class">The Element class</a></li><li class="menu item"><a href="tutorial.html#the-elementtree-class">The ElementTree class</a></li><li class="menu item"><a href="tutorial.html#parsing-from-strings-and-files">Parsing from strings and files</a></li><li class="menu item"><a href="tutorial.html#namespaces">Namespaces</a></li><li class="menu item"><a href="tutorial.html#the-e-factory">The E-factory</a></li><li class="menu item"><a href="tutorial.html#elementpath">ElementPath</a></li></ul></li></ul><ul class="menu foreign" id="api index-menu"><li class="menu title"><a href="api/index.html">API reference</a></li></ul><ul class="menu foreign" id="api-menu"><li class="menu title"><a href="api.html">APIs specific to lxml.etree</a><ul class="submenu"><li class="menu item"><a href="api.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="api.html#other-element-apis">Other Element APIs</a></li><li class="menu item"><a href="api.html#trees-and-documents">Trees and Documents</a></li><li class="menu item"><a href="api.html#iteration">Iteration</a></li><li class="menu item"><a href="api.html#error-handling-on-exceptions">Error handling on exceptions</a></li><li class="menu item"><a href="api.html#error-logging">Error logging</a></li><li class="menu item"><a href="api.html#serialisation">Serialisation</a></li><li class="menu item"><a href="api.html#cdata">CDATA</a></li><li class="menu item"><a href="api.html#xinclude-and-elementinclude">XInclude and ElementInclude</a></li><li class="menu item"><a href="api.html#write-c14n-on-elementtree">write_c14n on ElementTree</a></li></ul></li></ul><ul class="menu current" id="parsing-menu"><li class="menu title"><a href="parsing.html">Parsing XML and HTML with lxml</a><ul class="submenu"><li class="menu item"><a href="parsing.html#parsers">Parsers</a></li><li class="menu item"><a href="parsing.html#the-target-parser-interface">The target parser interface</a></li><li class="menu item"><a href="parsing.html#the-feed-parser-interface">The feed parser interface</a></li><li class="menu item"><a href="parsing.html#iterparse-and-iterwalk">iterparse and iterwalk</a></li><li class="menu item"><a href="parsing.html#python-unicode-strings">Python unicode strings</a></li></ul></li></ul><ul class="menu foreign" id="validation-menu"><li class="menu title"><a href="validation.html">Validation with lxml</a><ul class="submenu"><li class="menu item"><a href="validation.html#validation-at-parse-time">Validation at parse time</a></li><li class="menu item"><a href="validation.html#dtd">DTD</a></li><li class="menu item"><a href="validation.html#relaxng">RelaxNG</a></li><li class="menu item"><a href="validation.html#xmlschema">XMLSchema</a></li><li class="menu item"><a href="validation.html#schematron">Schematron</a></li></ul></li></ul><ul class="menu foreign" id="xpathxslt-menu"><li class="menu title"><a href="xpathxslt.html">XPath and XSLT with lxml</a><ul class="submenu"><li class="menu item"><a href="xpathxslt.html#xpath">XPath</a></li><li class="menu item"><a href="xpathxslt.html#xslt">XSLT</a></li></ul></li></ul><ul class="menu foreign" id="objectify-menu"><li class="menu title"><a href="objectify.html">lxml.objectify</a><ul class="submenu"><li class="menu item"><a href="objectify.html#the-lxml-objectify-api">The lxml.objectify API</a></li><li class="menu item"><a href="objectify.html#asserting-a-schema">Asserting a Schema</a></li><li class="menu item"><a href="objectify.html#objectpath">ObjectPath</a></li><li class="menu item"><a href="objectify.html#python-data-types">Python data types</a></li><li class="menu item"><a href="objectify.html#how-data-types-are-matched">How data types are matched</a></li><li class="menu item"><a href="objectify.html#what-is-different-from-lxml-etree">What is different from lxml.etree?</a></li></ul></li></ul><ul class="menu foreign" id="lxmlhtml-menu"><li class="menu title"><a href="lxmlhtml.html">lxml.html</a><ul class="submenu"><li class="menu item"><a href="lxmlhtml.html#parsing-html">Parsing HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-element-methods">HTML Element Methods</a></li><li class="menu item"><a href="lxmlhtml.html#running-html-doctests">Running HTML doctests</a></li><li class="menu item"><a href="lxmlhtml.html#creating-html-with-the-e-factory">Creating HTML with the E-factory</a></li><li class="menu item"><a href="lxmlhtml.html#working-with-links">Working with links</a></li><li class="menu item"><a href="lxmlhtml.html#forms">Forms</a></li><li class="menu item"><a href="lxmlhtml.html#cleaning-up-html">Cleaning up HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-diff">HTML Diff</a></li><li class="menu item"><a href="lxmlhtml.html#examples">Examples</a></li></ul></li></ul><ul class="menu foreign" id="cssselect-menu"><li class="menu title"><a href="cssselect.html">lxml.cssselect</a><ul class="submenu"><li class="menu item"><a href="cssselect.html#the-cssselector-class">The CSSSelector class</a></li><li class="menu item"><a href="cssselect.html#css-selectors">CSS Selectors</a></li><li class="menu item"><a href="cssselect.html#namespaces">Namespaces</a></li><li class="menu item"><a href="cssselect.html#limitations">Limitations</a></li></ul></li></ul><ul class="menu foreign" id="elementsoup-menu"><li class="menu title"><a href="elementsoup.html">BeautifulSoup Parser</a><ul class="submenu"><li class="menu item"><a href="elementsoup.html#parsing-with-the-soupparser">Parsing with the soupparser</a></li><li class="menu item"><a href="elementsoup.html#entity-handling">Entity handling</a></li><li class="menu item"><a href="elementsoup.html#using-soupparser-as-a-fallback">Using soupparser as a fallback</a></li></ul></li></ul></li></ul><ul id="Extending lxml-section"><li><span class="section title">Extending lxml</span><ul class="menu foreign" id="resolvers-menu"><li class="menu title"><a href="resolvers.html">Document loading and URL resolving</a><ul class="submenu"><li class="menu item"><a href="resolvers.html#uri-resolvers">URI Resolvers</a></li><li class="menu item"><a href="resolvers.html#document-loading-in-context">Document loading in context</a></li><li class="menu item"><a href="resolvers.html#i-o-access-control-in-xslt">I/O access control in XSLT</a></li></ul></li></ul><ul class="menu foreign" id="extensions-menu"><li class="menu title"><a href="extensions.html">Python extensions for XPath and XSLT</a><ul class="submenu"><li class="menu item"><a href="extensions.html#xpath-extension-functions">XPath Extension functions</a></li><li class="menu item"><a href="extensions.html#xslt-extension-elements">XSLT extension elements</a></li></ul></li></ul><ul class="menu foreign" id="element classes-menu"><li class="menu title"><a href="element_classes.html">Using custom Element classes in lxml</a><ul class="submenu"><li class="menu item"><a href="element_classes.html#element-initialization">Element initialization</a></li><li class="menu item"><a href="element_classes.html#setting-up-a-class-lookup-scheme">Setting up a class lookup scheme</a></li><li class="menu item"><a href="element_classes.html#implementing-namespaces">Implementing namespaces</a></li></ul></li></ul><ul class="menu foreign" id="sax-menu"><li class="menu title"><a href="sax.html">Sax support</a><ul class="submenu"><li class="menu item"><a href="sax.html#building-a-tree-from-sax-events">Building a tree from SAX events</a></li><li class="menu item"><a href="sax.html#producing-sax-events-from-an-elementtree-or-element">Producing SAX events from an ElementTree or Element</a></li><li class="menu item"><a href="sax.html#interfacing-with-pulldom-minidom">Interfacing with pulldom/minidom</a></li></ul></li></ul><ul class="menu foreign" id="capi-menu"><li class="menu title"><a href="capi.html">The public C-API of lxml.etree</a><ul class="submenu"><li class="menu item"><a href="capi.html#writing-external-modules-in-cython">Writing external modules in Cython</a></li><li class="menu item"><a href="capi.html#writing-external-modules-in-c">Writing external modules in C</a></li></ul></li></ul></li></ul><ul id="Developing lxml-section"><li><span class="section title">Developing lxml</span><ul class="menu foreign" id="build-menu"><li class="menu title"><a href="build.html">How to build lxml from source</a><ul class="submenu"><li class="menu item"><a href="build.html#cython">Cython</a></li><li class="menu item"><a href="build.html#subversion">Subversion</a></li><li class="menu item"><a href="build.html#setuptools">Setuptools</a></li><li class="menu item"><a href="build.html#running-the-tests-and-reporting-errors">Running the tests and reporting errors</a></li><li class="menu item"><a href="build.html#contributing-an-egg">Contributing an egg</a></li><li class="menu item"><a href="build.html#providing-newer-library-versions-on-mac-os-x">Providing newer library versions on Mac-OS X</a></li><li class="menu item"><a href="build.html#static-linking-on-windows">Static linking on Windows</a></li><li class="menu item"><a href="build.html#building-debian-packages-from-svn-sources">Building Debian packages from SVN sources</a></li></ul></li></ul><ul class="menu foreign" id="lxml source howto-menu"><li class="menu title"><a href="lxml-source-howto.html">How to read the source of lxml</a><ul class="submenu"><li class="menu item"><a href="lxml-source-howto.html#what-is-cython">What is Cython?</a></li><li class="menu item"><a href="lxml-source-howto.html#where-to-start">Where to start?</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="lxml-source-howto.html#python-modules">Python modules</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-objectify">lxml.objectify</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-html">lxml.html</a></li></ul></li></ul><ul class="menu foreign" id="changes 2 1 5-menu"><li class="menu title"><a href="changes-2.1.5.html">Release Changelog</a></li></ul><ul class="menu foreign" id="credits-menu"><li class="menu title"><a href="credits.html">Credits</a><ul class="submenu"><li class="menu item"><a href="credits.html#main-contributors">Main contributors</a></li><li class="menu item"><a href="credits.html#special-thanks-goes-to">Special thanks goes to:</a></li></ul></li></ul></li></ul></div><h1 class="title">Parsing XML and HTML with lxml</h1>
11
<div class="sidemenu"><ul id="lxml-section"><li><span class="section title">lxml</span><ul class="menu foreign" id="index-menu"><li class="menu title"><a href="index.html">lxml</a><ul class="submenu"><li class="menu item"><a href="index.html#introduction">Introduction</a></li><li class="menu item"><a href="index.html#documentation">Documentation</a></li><li class="menu item"><a href="index.html#download">Download</a></li><li class="menu item"><a href="index.html#mailing-list">Mailing list</a></li><li class="menu item"><a href="index.html#bug-tracker">Bug tracker</a></li><li class="menu item"><a href="index.html#license">License</a></li><li class="menu item"><a href="index.html#old-versions">Old Versions</a></li></ul></li></ul><ul class="menu foreign" id="intro-menu"><li class="menu title"><a href="intro.html">Why lxml?</a><ul class="submenu"><li class="menu item"><a href="intro.html#motto">Motto</a></li><li class="menu item"><a href="intro.html#aims">Aims</a></li></ul></li></ul><ul class="menu foreign" id="installation-menu"><li class="menu title"><a href="installation.html">Installing lxml</a><ul class="submenu"><li class="menu item"><a href="installation.html#requirements">Requirements</a></li><li class="menu item"><a href="installation.html#installation">Installation</a></li><li class="menu item"><a href="installation.html#building-lxml-from-sources">Building lxml from sources</a></li><li class="menu item"><a href="installation.html#ms-windows">MS Windows</a></li><li class="menu item"><a href="installation.html#macos-x">MacOS-X</a></li></ul></li></ul><ul class="menu foreign" id="lxml2-menu"><li class="menu title"><a href="lxml2.html">What's new in lxml 2.0?</a><ul class="submenu"><li class="menu item"><a href="lxml2.html#changes-in-etree-and-objectify">Changes in etree and objectify</a></li><li class="menu item"><a href="lxml2.html#new-modules">New modules</a></li></ul></li></ul><ul class="menu foreign" id="performance-menu"><li class="menu title"><a href="performance.html">Benchmarks and Speed</a><ul class="submenu"><li class="menu item"><a href="performance.html#general-notes">General notes</a></li><li class="menu item"><a href="performance.html#how-to-read-the-timings">How to read the timings</a></li><li class="menu item"><a href="performance.html#parsing-and-serialising">Parsing and Serialising</a></li><li class="menu item"><a href="performance.html#the-elementtree-api">The ElementTree API</a></li><li class="menu item"><a href="performance.html#xpath">XPath</a></li><li class="menu item"><a href="performance.html#a-longer-example">A longer example</a></li><li class="menu item"><a href="performance.html#lxml-objectify">lxml.objectify</a></li></ul></li></ul><ul class="menu foreign" id="compatibility-menu"><li class="menu title"><a href="compatibility.html">ElementTree compatibility of lxml.etree</a></li></ul><ul class="menu foreign" id="FAQ-menu"><li class="menu title"><a href="FAQ.html">lxml FAQ - Frequently Asked Questions</a><ul class="submenu"><li class="menu item"><a href="FAQ.html#general-questions">General Questions</a></li><li class="menu item"><a href="FAQ.html#installation">Installation</a></li><li class="menu item"><a href="FAQ.html#contributing">Contributing</a></li><li class="menu item"><a href="FAQ.html#bugs">Bugs</a></li><li class="menu item"><a href="FAQ.html#threading">Threading</a></li><li class="menu item"><a href="FAQ.html#parsing-and-serialisation">Parsing and Serialisation</a></li><li class="menu item"><a href="FAQ.html#xpath-and-document-traversal">XPath and Document Traversal</a></li></ul></li></ul></li></ul><ul id="Developing with lxml-section"><li><span class="section title">Developing with lxml</span><ul class="menu foreign" id="tutorial-menu"><li class="menu title"><a href="tutorial.html">The lxml.etree Tutorial</a><ul class="submenu"><li class="menu item"><a href="tutorial.html#the-element-class">The Element class</a></li><li class="menu item"><a href="tutorial.html#the-elementtree-class">The ElementTree class</a></li><li class="menu item"><a href="tutorial.html#parsing-from-strings-and-files">Parsing from strings and files</a></li><li class="menu item"><a href="tutorial.html#namespaces">Namespaces</a></li><li class="menu item"><a href="tutorial.html#the-e-factory">The E-factory</a></li><li class="menu item"><a href="tutorial.html#elementpath">ElementPath</a></li></ul></li></ul><ul class="menu foreign" id="api index-menu"><li class="menu title"><a href="api/index.html">API reference</a></li></ul><ul class="menu foreign" id="api-menu"><li class="menu title"><a href="api.html">APIs specific to lxml.etree</a><ul class="submenu"><li class="menu item"><a href="api.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="api.html#other-element-apis">Other Element APIs</a></li><li class="menu item"><a href="api.html#trees-and-documents">Trees and Documents</a></li><li class="menu item"><a href="api.html#iteration">Iteration</a></li><li class="menu item"><a href="api.html#error-handling-on-exceptions">Error handling on exceptions</a></li><li class="menu item"><a href="api.html#error-logging">Error logging</a></li><li class="menu item"><a href="api.html#serialisation">Serialisation</a></li><li class="menu item"><a href="api.html#cdata">CDATA</a></li><li class="menu item"><a href="api.html#xinclude-and-elementinclude">XInclude and ElementInclude</a></li><li class="menu item"><a href="api.html#write-c14n-on-elementtree">write_c14n on ElementTree</a></li></ul></li></ul><ul class="menu current" id="parsing-menu"><li class="menu title"><a href="parsing.html">Parsing XML and HTML with lxml</a><ul class="submenu"><li class="menu item"><a href="parsing.html#parsers">Parsers</a></li><li class="menu item"><a href="parsing.html#the-target-parser-interface">The target parser interface</a></li><li class="menu item"><a href="parsing.html#the-feed-parser-interface">The feed parser interface</a></li><li class="menu item"><a href="parsing.html#iterparse-and-iterwalk">iterparse and iterwalk</a></li><li class="menu item"><a href="parsing.html#python-unicode-strings">Python unicode strings</a></li></ul></li></ul><ul class="menu foreign" id="validation-menu"><li class="menu title"><a href="validation.html">Validation with lxml</a><ul class="submenu"><li class="menu item"><a href="validation.html#validation-at-parse-time">Validation at parse time</a></li><li class="menu item"><a href="validation.html#dtd">DTD</a></li><li class="menu item"><a href="validation.html#relaxng">RelaxNG</a></li><li class="menu item"><a href="validation.html#xmlschema">XMLSchema</a></li><li class="menu item"><a href="validation.html#schematron">Schematron</a></li></ul></li></ul><ul class="menu foreign" id="xpathxslt-menu"><li class="menu title"><a href="xpathxslt.html">XPath and XSLT with lxml</a><ul class="submenu"><li class="menu item"><a href="xpathxslt.html#xpath">XPath</a></li><li class="menu item"><a href="xpathxslt.html#xslt">XSLT</a></li></ul></li></ul><ul class="menu foreign" id="objectify-menu"><li class="menu title"><a href="objectify.html">lxml.objectify</a><ul class="submenu"><li class="menu item"><a href="objectify.html#the-lxml-objectify-api">The lxml.objectify API</a></li><li class="menu item"><a href="objectify.html#asserting-a-schema">Asserting a Schema</a></li><li class="menu item"><a href="objectify.html#objectpath">ObjectPath</a></li><li class="menu item"><a href="objectify.html#python-data-types">Python data types</a></li><li class="menu item"><a href="objectify.html#how-data-types-are-matched">How data types are matched</a></li><li class="menu item"><a href="objectify.html#what-is-different-from-lxml-etree">What is different from lxml.etree?</a></li></ul></li></ul><ul class="menu foreign" id="lxmlhtml-menu"><li class="menu title"><a href="lxmlhtml.html">lxml.html</a><ul class="submenu"><li class="menu item"><a href="lxmlhtml.html#parsing-html">Parsing HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-element-methods">HTML Element Methods</a></li><li class="menu item"><a href="lxmlhtml.html#running-html-doctests">Running HTML doctests</a></li><li class="menu item"><a href="lxmlhtml.html#creating-html-with-the-e-factory">Creating HTML with the E-factory</a></li><li class="menu item"><a href="lxmlhtml.html#working-with-links">Working with links</a></li><li class="menu item"><a href="lxmlhtml.html#forms">Forms</a></li><li class="menu item"><a href="lxmlhtml.html#cleaning-up-html">Cleaning up HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-diff">HTML Diff</a></li><li class="menu item"><a href="lxmlhtml.html#examples">Examples</a></li></ul></li></ul><ul class="menu foreign" id="cssselect-menu"><li class="menu title"><a href="cssselect.html">lxml.cssselect</a><ul class="submenu"><li class="menu item"><a href="cssselect.html#the-cssselector-class">The CSSSelector class</a></li><li class="menu item"><a href="cssselect.html#css-selectors">CSS Selectors</a></li><li class="menu item"><a href="cssselect.html#namespaces">Namespaces</a></li><li class="menu item"><a href="cssselect.html#limitations">Limitations</a></li></ul></li></ul><ul class="menu foreign" id="elementsoup-menu"><li class="menu title"><a href="elementsoup.html">BeautifulSoup Parser</a><ul class="submenu"><li class="menu item"><a href="elementsoup.html#parsing-with-the-soupparser">Parsing with the soupparser</a></li><li class="menu item"><a href="elementsoup.html#entity-handling">Entity handling</a></li><li class="menu item"><a href="elementsoup.html#using-soupparser-as-a-fallback">Using soupparser as a fallback</a></li><li class="menu item"><a href="elementsoup.html#using-only-the-encoding-detection">Using only the encoding detection</a></li></ul></li></ul><ul class="menu foreign" id="html5parser-menu"><li class="menu title"><a href="html5parser.html">html5lib Parser</a><ul class="submenu"><li class="menu item"><a href="html5parser.html#differences-to-regular-html-parsing">Differences to regular HTML parsing</a></li><li class="menu item"><a href="html5parser.html#function-reference">Function Reference</a></li></ul></li></ul></li></ul><ul id="Extending lxml-section"><li><span class="section title">Extending lxml</span><ul class="menu foreign" id="resolvers-menu"><li class="menu title"><a href="resolvers.html">Document loading and URL resolving</a><ul class="submenu"><li class="menu item"><a href="resolvers.html#uri-resolvers">URI Resolvers</a></li><li class="menu item"><a href="resolvers.html#document-loading-in-context">Document loading in context</a></li><li class="menu item"><a href="resolvers.html#i-o-access-control-in-xslt">I/O access control in XSLT</a></li></ul></li></ul><ul class="menu foreign" id="extensions-menu"><li class="menu title"><a href="extensions.html">Python extensions for XPath and XSLT</a><ul class="submenu"><li class="menu item"><a href="extensions.html#xpath-extension-functions">XPath Extension functions</a></li><li class="menu item"><a href="extensions.html#xslt-extension-elements">XSLT extension elements</a></li></ul></li></ul><ul class="menu foreign" id="element classes-menu"><li class="menu title"><a href="element_classes.html">Using custom Element classes in lxml</a><ul class="submenu"><li class="menu item"><a href="element_classes.html#background-on-element-proxies">Background on Element proxies</a></li><li class="menu item"><a href="element_classes.html#element-initialization">Element initialization</a></li><li class="menu item"><a href="element_classes.html#setting-up-a-class-lookup-scheme">Setting up a class lookup scheme</a></li><li class="menu item"><a href="element_classes.html#generating-xml-with-custom-classes">Generating XML with custom classes</a></li><li class="menu item"><a href="element_classes.html#implementing-namespaces">Implementing namespaces</a></li></ul></li></ul><ul class="menu foreign" id="sax-menu"><li class="menu title"><a href="sax.html">Sax support</a><ul class="submenu"><li class="menu item"><a href="sax.html#building-a-tree-from-sax-events">Building a tree from SAX events</a></li><li class="menu item"><a href="sax.html#producing-sax-events-from-an-elementtree-or-element">Producing SAX events from an ElementTree or Element</a></li><li class="menu item"><a href="sax.html#interfacing-with-pulldom-minidom">Interfacing with pulldom/minidom</a></li></ul></li></ul><ul class="menu foreign" id="capi-menu"><li class="menu title"><a href="capi.html">The public C-API of lxml.etree</a><ul class="submenu"><li class="menu item"><a href="capi.html#writing-external-modules-in-cython">Writing external modules in Cython</a></li><li class="menu item"><a href="capi.html#writing-external-modules-in-c">Writing external modules in C</a></li></ul></li></ul></li></ul><ul id="Developing lxml-section"><li><span class="section title">Developing lxml</span><ul class="menu foreign" id="build-menu"><li class="menu title"><a href="build.html">How to build lxml from source</a><ul class="submenu"><li class="menu item"><a href="build.html#cython">Cython</a></li><li class="menu item"><a href="build.html#subversion">Subversion</a></li><li class="menu item"><a href="build.html#setuptools">Setuptools</a></li><li class="menu item"><a href="build.html#running-the-tests-and-reporting-errors">Running the tests and reporting errors</a></li><li class="menu item"><a href="build.html#building-an-egg">Building an egg</a></li><li class="menu item"><a href="build.html#building-lxml-on-macos-x">Building lxml on MacOS-X</a></li><li class="menu item"><a href="build.html#static-linking-on-windows">Static linking on Windows</a></li><li class="menu item"><a href="build.html#building-debian-packages-from-svn-sources">Building Debian packages from SVN sources</a></li></ul></li></ul><ul class="menu foreign" id="lxml source howto-menu"><li class="menu title"><a href="lxml-source-howto.html">How to read the source of lxml</a><ul class="submenu"><li class="menu item"><a href="lxml-source-howto.html#what-is-cython">What is Cython?</a></li><li class="menu item"><a href="lxml-source-howto.html#where-to-start">Where to start?</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="lxml-source-howto.html#python-modules">Python modules</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-objectify">lxml.objectify</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-html">lxml.html</a></li></ul></li></ul><ul class="menu foreign" id="changes 2 2 4-menu"><li class="menu title"><a href="changes-2.2.4.html">Release Changelog</a></li></ul><ul class="menu foreign" id="credits-menu"><li class="menu title"><a href="credits.html">Credits</a><ul class="submenu"><li class="menu item"><a href="credits.html#main-contributors">Main contributors</a></li><li class="menu item"><a href="credits.html#special-thanks-goes-to">Special thanks goes to:</a></li></ul></li></ul></li></ul></div><h1 class="title">Parsing XML and HTML with lxml</h1>
12
13
<p>lxml provides a very simple and powerful API for parsing XML and HTML. It
13
14
supports one-step parsing as well as step-by-step parsing using an
14
15
event-driven API (currently only for XML).</p>
15
<div class="contents topic">
16
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
16
<div class="contents topic" id="contents">
17
<p class="topic-title first">Contents</p>
17
18
<ul class="simple">
18
<li><a class="reference" href="#parsers" id="id1" name="id1">Parsers</a><ul>
19
<li><a class="reference" href="#parser-options" id="id2" name="id2">Parser options</a></li>
20
<li><a class="reference" href="#error-log" id="id3" name="id3">Error log</a></li>
21
<li><a class="reference" href="#parsing-html" id="id4" name="id4">Parsing HTML</a></li>
22
<li><a class="reference" href="#doctype-information" id="id5" name="id5">Doctype information</a></li>
25
<li><a class="reference" href="#the-target-parser-interface" id="id6" name="id6">The target parser interface</a></li>
26
<li><a class="reference" href="#the-feed-parser-interface" id="id7" name="id7">The feed parser interface</a></li>
27
<li><a class="reference" href="#iterparse-and-iterwalk" id="id8" name="id8">iterparse and iterwalk</a><ul>
28
<li><a class="reference" href="#selective-tag-events" id="id9" name="id9">Selective tag events</a></li>
29
<li><a class="reference" href="#comments-and-pis" id="id10" name="id10">Comments and PIs</a></li>
30
<li><a class="reference" href="#modifying-the-tree" id="id11" name="id11">Modifying the tree</a></li>
31
<li><a class="reference" href="#iterwalk" id="id12" name="id12">iterwalk</a></li>
34
<li><a class="reference" href="#python-unicode-strings" id="id13" name="id13">Python unicode strings</a><ul>
35
<li><a class="reference" href="#serialising-to-unicode-strings" id="id14" name="id14">Serialising to Unicode strings</a></li>
19
<li><a class="reference internal" href="#parsers" id="id1">Parsers</a><ul>
20
<li><a class="reference internal" href="#parser-options" id="id2">Parser options</a></li>
21
<li><a class="reference internal" href="#error-log" id="id3">Error log</a></li>
22
<li><a class="reference internal" href="#parsing-html" id="id4">Parsing HTML</a></li>
23
<li><a class="reference internal" href="#doctype-information" id="id5">Doctype information</a></li>
26
<li><a class="reference internal" href="#the-target-parser-interface" id="id6">The target parser interface</a></li>
27
<li><a class="reference internal" href="#the-feed-parser-interface" id="id7">The feed parser interface</a></li>
28
<li><a class="reference internal" href="#iterparse-and-iterwalk" id="id8">iterparse and iterwalk</a><ul>
29
<li><a class="reference internal" href="#selective-tag-events" id="id9">Selective tag events</a></li>
30
<li><a class="reference internal" href="#comments-and-pis" id="id10">Comments and PIs</a></li>
31
<li><a class="reference internal" href="#modifying-the-tree" id="id11">Modifying the tree</a></li>
32
<li><a class="reference internal" href="#iterwalk" id="id12">iterwalk</a></li>
35
<li><a class="reference internal" href="#python-unicode-strings" id="id13">Python unicode strings</a><ul>
36
<li><a class="reference internal" href="#serialising-to-unicode-strings" id="id14">Serialising to Unicode strings</a></li>
40
41
<p>The usual setup procedure:</p>
41
<div class="syntax"><pre><span class="gp">>>> </span><span class="k">from</span> <span class="nn">lxml</span> <span class="k">import</span> <span class="n">etree</span>
42
<div class="syntax"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">lxml</span> <span class="kn">import</span> <span class="n">etree</span>
44
<h1><a id="parsers" name="parsers">Parsers</a></h1>
44
<div class="section" id="parsers">
45
46
<p>Parsers are represented by parser objects. There is support for parsing both
46
47
XML and (broken) HTML. Note that XHTML is best parsed as XML, parsing it with
47
48
the HTML parser can lead to unexpected results. Here is a simple example for
88
89
the default attributes from it</li>
89
90
<li>dtd_validation - validate while parsing (if a DTD was referenced)</li>
90
91
<li>load_dtd - load and parse the DTD while parsing (no validation is performed)</li>
91
<li>no_network - prevent network access when looking up external documents</li>
92
<li>no_network - prevent network access when looking up external
93
documents (on by default)</li>
92
94
<li>ns_clean - try to clean up redundant namespace declarations</li>
93
95
<li>recover - try hard to parse through broken XML</li>
94
96
<li>remove_blank_text - discard blank text nodes between tags</li>
95
97
<li>remove_comments - discard comments</li>
98
<li>remove_pis - discard processing instructions</li>
99
<li>strip_cdata - replace CDATA sections by normal text content (on by
101
<li>resolve_entities - replace entities by their text value (on by
103
<li>huge_tree - disable security restrictions and support very deep trees
104
and very long text content (only affects libxml2 2.7+)</li>
96
105
<li>compact - use compact storage for short text content (on by default)</li>
100
<h2><a id="error-log" name="error-log">Error log</a></h2>
108
<div class="section" id="error-log">
101
110
<p>Parsers have an <tt class="docutils literal"><span class="pre">error_log</span></tt> property that lists the errors of the
102
111
last parser run:</p>
103
112
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XMLParser</span><span class="p">()</span>
107
116
<span class="gp">>>> </span><span class="n">tree</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">"<root></b>"</span><span class="p">,</span> <span class="n">parser</span><span class="p">)</span>
108
117
<span class="gt">Traceback (most recent call last):</span>
109
<span class="c">...</span>
118
<span class="c">...</span>
110
119
<span class="nc">lxml.etree.XMLSyntaxError: Opening and ending tag mismatch</span>: <span class="n-Identifier">root line 1 and b, line 1, column 11</span>
112
121
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">error_log</span><span class="p">))</span>
113
122
<span class="go">1</span>
115
<span class="gp">>>> </span><span class="n">error</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">error_log</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span>
124
<span class="gp">>>> </span><span class="n">error</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">error_log</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
116
125
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">error</span><span class="o">.</span><span class="n">message</span><span class="p">)</span>
117
126
<span class="go">Opening and ending tag mismatch: root line 1 and b</span>
118
127
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">error</span><span class="o">.</span><span class="n">line</span><span class="p">)</span>
121
130
<span class="go">11</span>
124
<div class="section">
125
<h2><a id="parsing-html" name="parsing-html">Parsing HTML</a></h2>
126
<p>HTML parsing is similarly simple. The parsers have a <tt class="docutils literal"><span class="pre">recover</span></tt> keyword
127
argument that the HTMLParser sets by default. It lets libxml2 try its best to
128
return something usable without raising an exception. You should use libxml2
129
version 2.6.21 or newer to take advantage of this feature:</p>
133
<div class="section" id="parsing-html">
134
<h2>Parsing HTML</h2>
135
<p>HTML parsing is similarly simple. The parsers have a <tt class="docutils literal"><span class="pre">recover</span></tt>
136
keyword argument that the HTMLParser sets by default. It lets libxml2
137
try its best to return a valid HTML tree with all content it can
138
manage to parse. It will not raise an exception on parser errors.
139
You should use libxml2 version 2.6.21 or newer to take advantage of
130
141
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">broken_html</span> <span class="o">=</span> <span class="s">"<html><head><title>test<body><h1>page title</h3>"</span>
132
143
<span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">HTMLParser</span><span class="p">()</span>
165
176
parser may have to drop seriously broken parts when struggling to keep
166
177
parsing. Especially misplaced meta tags can suffer from this, which may lead
167
178
to encoding problems.</p>
179
<p>Note that the result is a valid HTML tree, but it may not be a
180
well-formed XML tree. For example, XML forbids double hyphens in
181
comments, which the HTML parser will happily accept in recovery mode.
182
Therefore, if your goal is to serialise an HTML document as an
183
XML/XHTML document after parsing, you may have to apply some manual
184
preprocessing first.</p>
169
<div class="section">
170
<h2><a id="doctype-information" name="doctype-information">Doctype information</a></h2>
186
<div class="section" id="doctype-information">
187
<h2>Doctype information</h2>
171
188
<p>The use of the libxml2 parsers makes some additional information available at
172
189
the API level. Currently, ElementTree objects can access the DOCTYPE
173
190
information provided by a parsed document, as well as the XML version and the
197
<div class="section">
198
<h1><a id="the-target-parser-interface" name="the-target-parser-interface">The target parser interface</a></h1>
199
<p><a class="reference" href="http://effbot.org/elementtree/elementtree-xmlparser.htm">As in ElementTree</a>, and similar to a SAX event handler, you can pass
214
<div class="section" id="the-target-parser-interface">
215
<h1>The target parser interface</h1>
216
<p><a class="reference external" href="http://effbot.org/elementtree/elementtree-xmlparser.htm">As in ElementTree</a>, and similar to a SAX event handler, you can pass
200
217
a target object to the parser:</p>
201
218
<div class="syntax"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">EchoTarget</span><span class="p">:</span>
202
219
<span class="gp">... </span> <span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">attrib</span><span class="p">):</span>
214
231
<span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XMLParser</span><span class="p">(</span><span class="n">target</span> <span class="o">=</span> <span class="n">EchoTarget</span><span class="p">())</span>
216
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">"<element>some<!--comment-->text</element>"</span><span class="p">,</span> <span class="n">parser</span><span class="p">)</span>
217
<span class="go">start element {}</span>
218
<span class="go">data u'some'</span>
219
<span class="go">comment comment</span>
220
<span class="go">data u'text'</span>
221
<span class="go">end element</span>
222
<span class="go">close</span>
224
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
225
<span class="go">closed!</span>
227
<p>Note that the parser does <em>not</em> build a tree in this case. The result
228
of the parser run is whatever the target object returns from its
229
<tt class="docutils literal"><span class="pre">close()</span></tt> method. If you want to return an XML tree here, you have
230
to create it programmatically in the target object. An example for a
231
parser target that builds a tree is the <tt class="docutils literal"><span class="pre">TreeBuilder</span></tt>.</p>
233
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">"<element>some<!--comment-->text</element>"</span><span class="p">,</span>
234
<span class="gp">... </span> <span class="n">parser</span><span class="p">)</span>
235
<span class="go">start element {}</span>
236
<span class="go">data u'some'</span>
237
<span class="go">comment comment</span>
238
<span class="go">data u'text'</span>
239
<span class="go">end element</span>
240
<span class="go">close</span>
242
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
243
<span class="go">closed!</span>
245
<p>It is important for the <tt class="docutils literal"><span class="pre">.close()</span></tt> method to reset the parser target
246
to a usable state, so that you can reuse the parser as often as you
248
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">"<element>some<!--comment-->text</element>"</span><span class="p">,</span>
249
<span class="gp">... </span> <span class="n">parser</span><span class="p">)</span>
250
<span class="go">start element {}</span>
251
<span class="go">data u'some'</span>
252
<span class="go">comment comment</span>
253
<span class="go">data u'text'</span>
254
<span class="go">end element</span>
255
<span class="go">close</span>
257
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
258
<span class="go">closed!</span>
260
<p>Note that the parser does <em>not</em> build a tree when using a parser
261
target. The result of the parser run is whatever the target object
262
returns from its <tt class="docutils literal"><span class="pre">.close()</span></tt> method. If you want to return an XML
263
tree here, you have to create it programmatically in the target
264
object. An example for a parser target that builds a tree is the
265
<tt class="docutils literal"><span class="pre">TreeBuilder</span></tt>.</p>
233
267
<pre class="doctest-block">
234
268
>>> parser = etree.XMLParser(target = etree.TreeBuilder())
236
270
<pre class="doctest-block">
237
>>> result = etree.XML("<element>some<!--comment-->text</element>", parser)
271
>>> result = etree.XML("<element>some<!--comment-->text</element>",
239
274
<pre class="doctest-block">
240
275
>>> print(result.tag)
247
<div class="section">
248
<h1><a id="the-feed-parser-interface" name="the-feed-parser-interface">The feed parser interface</a></h1>
282
<div class="section" id="the-feed-parser-interface">
283
<h1>The feed parser interface</h1>
249
284
<p>Since lxml 2.0, the parsers have a feed parser interface that is
250
compatible to the <a class="reference" href="http://effbot.org/elementtree/elementtree-xmlparser.htm">ElementTree parsers</a>. You can use it to feed data
285
compatible to the <a class="reference external" href="http://effbot.org/elementtree/elementtree-xmlparser.htm">ElementTree parsers</a>. You can use it to feed data
251
286
into the parser in a controlled step-by-step way.</p>
252
287
<p>In lxml.etree, you can use both interfaces to a parser at the same
253
288
time: the <tt class="docutils literal"><span class="pre">parse()</span></tt> or <tt class="docutils literal"><span class="pre">XML()</span></tt> functions, and the feed parser
354
389
corresponding <tt class="docutils literal"><span class="pre">end-ns</span></tt> event does not have a value (None). It is
355
390
common practice to use a list as namespace stack and pop the last
356
391
entry on the 'end-ns' event.</p>
357
<div class="syntax"><pre><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">xml</span><span class="p">[:</span><span class="o">-</span><span class="mf">1</span><span class="p">])</span>
392
<div class="syntax"><pre><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">xml</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
358
393
<span class="go"><root></span>
359
394
<span class="go"> <element key='value'>text</element></span>
360
395
<span class="go"> <element>text</element>tail</span>
472
507
<span class="gp">... </span> <span class="c"># ... do something with the element</span>
473
508
<span class="gp">... </span> <span class="n">element</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span> <span class="c"># clean up children</span>
474
509
<span class="gp">... </span> <span class="k">while</span> <span class="n">element</span><span class="o">.</span><span class="n">getprevious</span><span class="p">()</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
475
<span class="gp">... </span> <span class="k">del</span> <span class="n">element</span><span class="o">.</span><span class="n">getparent</span><span class="p">()[</span><span class="mf">0</span><span class="p">]</span> <span class="c"># clean up preceding siblings</span>
510
<span class="gp">... </span> <span class="k">del</span> <span class="n">element</span><span class="o">.</span><span class="n">getparent</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span> <span class="c"># clean up preceding siblings</span>
477
512
<p>The <tt class="docutils literal"><span class="pre">while</span></tt> loop deletes multiple siblings in a row. This is only necessary
478
513
if you skipped over some of them using the <tt class="docutils literal"><span class="pre">tag</span></tt> keyword argument.
525
560
encoding themselves and thus lie about their real encoding:</p>
526
561
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">etree</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span> <span class="s">u'<?xml version="1.0" encoding="ASCII"?></span><span class="se">\n</span><span class="s">'</span> <span class="o">+</span> <span class="n">uxml</span> <span class="p">)</span>
527
562
<span class="gt">Traceback (most recent call last):</span>
528
<span class="c">...</span>
563
<span class="c">...</span>
529
564
<span class="nc">ValueError</span>: <span class="n-Identifier">Unicode strings with encoding declaration are not supported.</span>
531
566
<p>Similarly, you will get errors when you try the same with HTML data in a
532
567
unicode string that specifies a charset in a meta tag of the header. You
533
568
should generally avoid converting XML/HTML data to unicode before passing it
534
569
into the parsers. It is both slower and error prone.</p>
535
<div class="section">
536
<h2><a id="serialising-to-unicode-strings" name="serialising-to-unicode-strings">Serialising to Unicode strings</a></h2>
570
<div class="section" id="serialising-to-unicode-strings">
571
<h2>Serialising to Unicode strings</h2>
537
572
<p>To serialize the result, you would normally use the <tt class="docutils literal"><span class="pre">tostring()</span></tt>
538
573
module function, which serializes to plain ASCII by default or a
539
574
number of other byte encodings if asked for:</p>