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