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