Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / module-htmllib.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-htmlentitydefs.html" />
<link rel="prev" href="module-sgmllib.html" />
<link rel="parent" href="markup.html" />
<link rel="next" href="html-parser-objects.html" />
<meta name='aesop' content='information' />
<title>13.3 htmllib -- A parser for HTML documents</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.2 sgmllib "
href="module-sgmllib.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="13. Structured Markup Processing"
href="markup.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="13.3.1 HTMLParser Objects"
href="html-parser-objects.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-sgmllib.html">13.2 sgmllib </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">13. Structured Markup Processing</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="html-parser-objects.html">13.3.1 HTMLParser Objects</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0015300000000000000000">
13.3 <tt class="module">htmllib</tt> --
A parser for HTML documents</A>
</H1>
<P>
<A NAME="module-htmllib"></A>
<P>
<a id='l2h-4286' xml:id='l2h-4286'></a>
<P>
This module defines a class which can serve as a base for parsing text
files formatted in the HyperText Mark-up Language (HTML). The class
is not directly concerned with I/O -- it must be provided with input
in string form via a method, and makes calls to methods of a
``formatter'' object in order to produce output. The
<tt class="class">HTMLParser</tt> class is designed to be used as a base class for
other classes in order to add functionality, and allows most of its
methods to be extended or overridden. In turn, this class is derived
from and extends the <tt class="class">SGMLParser</tt> class defined in module
<tt class="module"><a href="module-sgmllib.html">sgmllib</a></tt><a id='l2h-4287' xml:id='l2h-4287'></a>. The <tt class="class">HTMLParser</tt>
implementation supports the HTML 2.0 language as described in
<a class="rfc" id='rfcref-91438' xml:id='rfcref-91438'
href="http://www.faqs.org/rfcs/rfc1866.html">RFC 1866</a>. Two implementations of formatter objects are provided in
the <tt class="module"><a href="module-formatter.html">formatter</a></tt><a id='l2h-4288' xml:id='l2h-4288'></a> module; refer to the
documentation for that module for information on the formatter
interface.
<a id='l2h-4283' xml:id='l2h-4283'></a>
<P>
The following is a summary of the interface defined by
<tt class="class">sgmllib.SGMLParser</tt>:
<P>
<UL>
<LI>The interface to feed data to an instance is through the <tt class="method">feed()</tt>
method, which takes a string argument. This can be called with as
little or as much text at a time as desired; "<tt class="samp">p.feed(a);
p.feed(b)</tt>" has the same effect as "<tt class="samp">p.feed(a+b)</tt>". When the data
contains complete HTML markup constructs, these are processed immediately;
incomplete constructs are saved in a buffer. To force processing of all
unprocessed data, call the <tt class="method">close()</tt> method.
<P>
For example, to parse the entire contents of a file, use:
<div class="verbatim"><pre>
parser.feed(open('myfile.html').read())
parser.close()
</pre></div>
<P>
</LI>
<LI>The interface to define semantics for HTML tags is very simple: derive
a class and define methods called <tt class="method">start_<var>tag</var>()</tt>,
<tt class="method">end_<var>tag</var>()</tt>, or <tt class="method">do_<var>tag</var>()</tt>. The parser will
call these at appropriate moments: <tt class="method">start_<var>tag</var></tt> or
<tt class="method">do_<var>tag</var>()</tt> is called when an opening tag of the form
<code>&lt;<var>tag</var> ...&gt;</code> is encountered; <tt class="method">end_<var>tag</var>()</tt> is called
when a closing tag of the form <code>&lt;<var>tag</var>&gt;</code> is encountered. If
an opening tag requires a corresponding closing tag, like <code>&lt;H1&gt;</code>
... <code>&lt;/H1&gt;</code>, the class should define the <tt class="method">start_<var>tag</var>()</tt>
method; if a tag requires no closing tag, like <code>&lt;P&gt;</code>, the class
should define the <tt class="method">do_<var>tag</var>()</tt> method.
<P>
</LI>
</UL>
<P>
The module defines a parser class and an exception:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-4284' xml:id='l2h-4284' class="class">HTMLParser</tt></b>(</nobr></td>
<td><var>formatter</var>)</td></tr></table></dt>
<dd>
This is the basic HTML parser class. It supports all entity names
required by the XHTML 1.0 Recommendation (<a class="url" href="http://www.w3.org/TR/xhtml1">http://www.w3.org/TR/xhtml1</a>).
It also defines handlers for all HTML 2.0 and many HTML 3.0 and 3.2 elements.
</dl>
<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4285' xml:id='l2h-4285' class="exception">HTMLParseError</tt></b></dt>
<dd>
Exception raised by the <tt class="class">HTMLParser</tt> class when it encounters an
error while parsing.
<span class="versionnote">New in version 2.4.</span>
</dd></dl>
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-formatter.html">formatter</a></tt>:</b>
<dd>Interface definition for transforming an
abstract flow of formatting events into
specific output events on writer objects.
</dl>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-HTMLParser.html">HTMLParser</a></tt>:</b>
<dd>Alternate HTML parser that offers a slightly
lower-level view of the input, but is
designed to work with XHTML, and does not
implement some of the SGML syntax not used in
``HTML as deployed'' and which isn't legal
for XHTML.
</dl>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-htmlentitydefs.html">htmlentitydefs</a></tt>:</b>
<dd>Definition of replacement text for XHTML 1.0
entities.
</dl>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-sgmllib.html">sgmllib</a></tt>:</b>
<dd>Base class for <tt class="class">HTMLParser</tt>.
</dl>
</div>
<P>
<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="html-parser-objects.html">13.3.1 HTMLParser Objects</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.2 sgmllib "
href="module-sgmllib.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="13. Structured Markup Processing"
href="markup.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="13.3.1 HTMLParser Objects"
href="html-parser-objects.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-sgmllib.html">13.2 sgmllib </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">13. Structured Markup Processing</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="html-parser-objects.html">13.3.1 HTMLParser Objects</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>