Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{htmllib} --- |
| 2 | A parser for HTML documents.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{htmllib} |
| 4 | |
| 5 | \modulesynopsis{A parser for HTML documents.} |
| 6 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 7 | \index{HTML} |
| 8 | \index{hypertext} |
| 9 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 10 | |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 11 | This module defines a class which can serve as a base for parsing text |
| 12 | files formatted in the HyperText Mark-up Language (HTML). The class |
| 13 | is not directly concerned with I/O --- it must be provided with input |
| 14 | in string form via a method, and makes calls to methods of a |
| 15 | ``formatter'' object in order to produce output. The |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 16 | \class{HTMLParser} class is designed to be used as a base class for |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 17 | other classes in order to add functionality, and allows most of its |
| 18 | methods to be extended or overridden. In turn, this class is derived |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 19 | from and extends the \class{SGMLParser} class defined in module |
| 20 | \module{sgmllib}\refstmodindex{sgmllib}. The \class{HTMLParser} |
| 21 | implementation supports the HTML 2.0 language as described in |
| 22 | \rfc{1866}. Two implementations of formatter objects are provided in |
| 23 | the \module{formatter}\refstmodindex{formatter} module; refer to the |
| 24 | documentation for that module for information on the formatter |
| 25 | interface. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 26 | \index{SGML} |
Fred Drake | 51375ae | 1998-03-12 14:39:09 +0000 | [diff] [blame] | 27 | \withsubitem{(in module sgmllib)}{\ttindex{SGMLParser}} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 28 | \index{formatter} |
| 29 | |
| 30 | The following is a summary of the interface defined by |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 31 | \class{sgmllib.SGMLParser}: |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 32 | |
| 33 | \begin{itemize} |
| 34 | |
| 35 | \item |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 36 | The interface to feed data to an instance is through the \method{feed()} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 37 | method, which takes a string argument. This can be called with as |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 38 | little or as much text at a time as desired; \samp{p.feed(a); |
| 39 | p.feed(b)} has the same effect as \samp{p.feed(a+b)}. When the data |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 40 | contains complete HTML tags, these are processed immediately; |
| 41 | incomplete elements are saved in a buffer. To force processing of all |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 42 | unprocessed data, call the \method{close()} method. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 43 | |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 44 | For example, to parse the entire contents of a file, use: |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 45 | \begin{verbatim} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 46 | parser.feed(open('myfile.html').read()) |
| 47 | parser.close() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 48 | \end{verbatim} |
Fred Drake | 51375ae | 1998-03-12 14:39:09 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 50 | \item |
| 51 | The interface to define semantics for HTML tags is very simple: derive |
| 52 | a class and define methods called \code{start_\var{tag}()}, |
| 53 | \code{end_\var{tag}()}, or \code{do_\var{tag}()}. The parser will |
| 54 | call these at appropriate moments: \code{start_\var{tag}} or |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 55 | \code{do_\var{tag}()} is called when an opening tag of the form |
| 56 | \code{<\var{tag} ...>} is encountered; \code{end_\var{tag}()} is called |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 57 | when a closing tag of the form \code{<\var{tag}>} is encountered. If |
| 58 | an opening tag requires a corresponding closing tag, like \code{<H1>} |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 59 | ... \code{</H1>}, the class should define the \code{start_\var{tag}()} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 60 | method; if a tag requires no closing tag, like \code{<P>}, the class |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 61 | should define the \code{do_\var{tag}()} method. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 62 | |
| 63 | \end{itemize} |
| 64 | |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 65 | The module defines a single class: |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 66 | |
Fred Drake | 51375ae | 1998-03-12 14:39:09 +0000 | [diff] [blame] | 67 | \begin{classdesc}{HTMLParser}{formatter} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 68 | This is the basic HTML parser class. It supports all entity names |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 69 | required by the HTML 2.0 specification (\rfc{1866}). It also defines |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 70 | handlers for all HTML 2.0 and many HTML 3.0 and 3.2 elements. |
Fred Drake | 51375ae | 1998-03-12 14:39:09 +0000 | [diff] [blame] | 71 | \end{classdesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 72 | |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 73 | In addition to tag methods, the \class{HTMLParser} class provides some |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 74 | additional methods and instance variables for use within tag methods. |
| 75 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 76 | \begin{memberdesc}{formatter} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 77 | This is the formatter instance associated with the parser. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 78 | \end{memberdesc} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 79 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 80 | \begin{memberdesc}{nofill} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 81 | Boolean flag which should be true when whitespace should not be |
| 82 | collapsed, or false when it should be. In general, this should only |
| 83 | be true when character data is to be treated as ``preformatted'' text, |
| 84 | as within a \code{<PRE>} element. The default value is false. This |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 85 | affects the operation of \method{handle_data()} and \method{save_end()}. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 86 | \end{memberdesc} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 87 | |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 88 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 89 | \begin{methoddesc}{anchor_bgn}{href, name, type} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 90 | This method is called at the start of an anchor region. The arguments |
| 91 | correspond to the attributes of the \code{<A>} tag with the same |
| 92 | names. The default implementation maintains a list of hyperlinks |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 93 | (defined by the \code{href} attribute) within the document. The list |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 94 | of hyperlinks is available as the data attribute \code{anchorlist}. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 95 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 96 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 97 | \begin{methoddesc}{anchor_end}{} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 98 | This method is called at the end of an anchor region. The default |
| 99 | implementation adds a textual footnote marker using an index into the |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 100 | list of hyperlinks created by \method{anchor_bgn()}. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 101 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 102 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 103 | \begin{methoddesc}{handle_image}{source, alt\optional{, ismap\optional{, align\optional{, width\optional{, height}}}}} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 104 | This method is called to handle images. The default implementation |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 105 | simply passes the \var{alt} value to the \method{handle_data()} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 106 | method. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 107 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 108 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 109 | \begin{methoddesc}{save_bgn}{} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 110 | Begins saving character data in a buffer instead of sending it to the |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 111 | formatter object. Retrieve the stored data via \method{save_end()}. |
| 112 | Use of the \method{save_bgn()} / \method{save_end()} pair may not be |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 113 | nested. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 114 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 115 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 116 | \begin{methoddesc}{save_end}{} |
Fred Drake | 58d7f69 | 1996-10-08 21:52:23 +0000 | [diff] [blame] | 117 | Ends buffering character data and returns all data saved since the |
Fred Drake | 526467c | 1998-02-10 21:42:27 +0000 | [diff] [blame] | 118 | preceeding call to \method{save_bgn()}. If the \code{nofill} flag is |
| 119 | false, whitespace is collapsed to single spaces. A call to this |
| 120 | method without a preceeding call to \method{save_bgn()} will raise a |
| 121 | \exception{TypeError} exception. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 122 | \end{methoddesc} |