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