annotate Doc/library/htmlparser.rst @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 7fc96d7ecf2e
children 8c1045b92b6f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47177
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 :mod:`HTMLParser` --- Simple HTML and XHTML parser
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 ==================================================
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5 .. module:: HTMLParser
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 :synopsis: A simple parser that can handle HTML and XHTML.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8 .. note::
47185
7fc96d7ecf2e Standardize renaming notes.
Georg Brandl <georg@python.org>
parents: 47177
diff changeset
9
7fc96d7ecf2e Standardize renaming notes.
Georg Brandl <georg@python.org>
parents: 47177
diff changeset
10 The :mod:`HTMLParser` module has been renamed to :mod:`html.parser` in Python
7fc96d7ecf2e Standardize renaming notes.
Georg Brandl <georg@python.org>
parents: 47177
diff changeset
11 3.0. The :term:`2to3` tool will automatically adapt imports when converting
7fc96d7ecf2e Standardize renaming notes.
Georg Brandl <georg@python.org>
parents: 47177
diff changeset
12 your sources to 3.0.
47177
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 .. versionadded:: 2.2
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 .. index::
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 single: HTML
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 single: XHTML
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 This module defines a class :class:`HTMLParser` which serves as the basis for
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 parsing text files formatted in HTML (HyperText Mark-up Language) and XHTML.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 Unlike the parser in :mod:`htmllib`, this parser is not based on the SGML parser
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 in :mod:`sgmllib`.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27 .. class:: HTMLParser()
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29 The :class:`HTMLParser` class is instantiated without arguments.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 An :class:`HTMLParser` instance is fed HTML data and calls handler functions when tags
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32 begin and end. The :class:`HTMLParser` class is meant to be overridden by the
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 user to provide a desired behavior.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 Unlike the parser in :mod:`htmllib`, this parser does not check that end tags
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 match start tags or call the end-tag handler for elements which are closed
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 implicitly by closing an outer element.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 An exception is defined as well:
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 .. exception:: HTMLParseError
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 Exception raised by the :class:`HTMLParser` class when it encounters an error
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 while parsing. This exception provides three attributes: :attr:`msg` is a brief
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 message explaining the error, :attr:`lineno` is the number of the line on which
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 the broken construct was detected, and :attr:`offset` is the number of
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 characters into the line at which the construct starts.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 :class:`HTMLParser` instances have the following methods:
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 .. method:: HTMLParser.reset()
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 Reset the instance. Loses all unprocessed data. This is called implicitly at
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 instantiation time.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 .. method:: HTMLParser.feed(data)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 Feed some text to the parser. It is processed insofar as it consists of
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 complete elements; incomplete data is buffered until more data is fed or
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 :meth:`close` is called.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 .. method:: HTMLParser.close()
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 Force processing of all buffered data as if it were followed by an end-of-file
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 mark. This method may be redefined by a derived class to define additional
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 processing at the end of the input, but the redefined version should always call
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
71 the :class:`HTMLParser` base class method :meth:`close`.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74 .. method:: HTMLParser.getpos()
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76 Return current line number and offset.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79 .. method:: HTMLParser.get_starttag_text()
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 Return the text of the most recently opened start tag. This should not normally
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 be needed for structured processing, but may be useful in dealing with HTML "as
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83 deployed" or for re-generating input with minimal changes (whitespace between
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84 attributes can be preserved, etc.).
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 .. method:: HTMLParser.handle_starttag(tag, attrs)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 This method is called to handle the start of a tag. It is intended to be
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90 overridden by a derived class; the base class implementation does nothing.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92 The *tag* argument is the name of the tag converted to lower case. The *attrs*
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
93 argument is a list of ``(name, value)`` pairs containing the attributes found
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 inside the tag's ``<>`` brackets. The *name* will be translated to lower case,
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 and quotes in the *value* have been removed, and character and entity references
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 have been replaced. For instance, for the tag ``<A
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97 HREF="http://www.cwi.nl/">``, this method would be called as
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98 ``handle_starttag('a', [('href', 'http://www.cwi.nl/')])``.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
99
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100 .. versionchanged:: 2.6
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
101 All entity references from :mod:`htmlentitydefs` are now replaced in the attribute
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102 values.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 .. method:: HTMLParser.handle_startendtag(tag, attrs)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107 Similar to :meth:`handle_starttag`, but called when the parser encounters an
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108 XHTML-style empty tag (``<a .../>``). This method may be overridden by
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 subclasses which require this particular lexical information; the default
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110 implementation simple calls :meth:`handle_starttag` and :meth:`handle_endtag`.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113 .. method:: HTMLParser.handle_endtag(tag)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 This method is called to handle the end tag of an element. It is intended to be
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 overridden by a derived class; the base class implementation does nothing. The
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117 *tag* argument is the name of the tag converted to lower case.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 .. method:: HTMLParser.handle_data(data)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 This method is called to process arbitrary data. It is intended to be
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 overridden by a derived class; the base class implementation does nothing.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 .. method:: HTMLParser.handle_charref(name)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
128 This method is called to process a character reference of the form ``&#ref;``.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 It is intended to be overridden by a derived class; the base class
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 implementation does nothing.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
132
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
133 .. method:: HTMLParser.handle_entityref(name)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
135 This method is called to process a general entity reference of the form
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 ``&name;`` where *name* is an general entity reference. It is intended to be
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 overridden by a derived class; the base class implementation does nothing.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
139
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
140 .. method:: HTMLParser.handle_comment(data)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
141
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
142 This method is called when a comment is encountered. The *comment* argument is
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
143 a string containing the text between the ``--`` and ``--`` delimiters, but not
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
144 the delimiters themselves. For example, the comment ``<!--text-->`` will cause
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
145 this method to be called with the argument ``'text'``. It is intended to be
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
146 overridden by a derived class; the base class implementation does nothing.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
147
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
148
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
149 .. method:: HTMLParser.handle_decl(decl)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
150
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
151 Method called when an SGML declaration is read by the parser. The *decl*
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
152 parameter will be the entire contents of the declaration inside the ``<!``...\
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
153 ``>`` markup. It is intended to be overridden by a derived class; the base
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
154 class implementation does nothing.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
155
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
156
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
157 .. method:: HTMLParser.handle_pi(data)
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
158
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
159 Method called when a processing instruction is encountered. The *data*
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
160 parameter will contain the entire processing instruction. For example, for the
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
161 processing instruction ``<?proc color='red'>``, this method would be called as
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
162 ``handle_pi("proc color='red'")``. It is intended to be overridden by a derived
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
163 class; the base class implementation does nothing.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
164
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
165 .. note::
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
166
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
167 The :class:`HTMLParser` class uses the SGML syntactic rules for processing
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
168 instructions. An XHTML processing instruction using the trailing ``'?'`` will
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
169 cause the ``'?'`` to be included in *data*.
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
170
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
171
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
172 .. _htmlparser-example:
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
173
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
174 Example HTML Parser Application
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
175 -------------------------------
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
176
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
177 As a basic example, below is a very basic HTML parser that uses the
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
178 :class:`HTMLParser` class to print out tags as they are encountered::
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
180 from HTMLParser import HTMLParser
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
181
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
182 class MyHTMLParser(HTMLParser):
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
183
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
184 def handle_starttag(self, tag, attrs):
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 print "Encountered the beginning of a %s tag" % tag
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187 def handle_endtag(self, tag):
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188 print "Encountered the end of a %s tag" % tag
454d38c1f47e revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
diff changeset
189