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