mirror of
https://github.com/python/cpython.git
synced 2025-08-19 00:00:48 +00:00

svn+ssh://pythondev@svn.python.org/python/branches/release27-maint ................ r83452 | georg.brandl | 2010-08-01 23:06:46 +0200 (So, 01 Aug 2010) | 25 lines Merged revisions 83226-83227,83229-83230,83232 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83226 | georg.brandl | 2010-07-29 16:17:12 +0200 (Do, 29 Jul 2010) | 1 line #1090076: explain the behavior of *vars* in get() better. ........ r83227 | georg.brandl | 2010-07-29 16:23:06 +0200 (Do, 29 Jul 2010) | 1 line Use Py_CLEAR(). ........ r83229 | georg.brandl | 2010-07-29 16:32:22 +0200 (Do, 29 Jul 2010) | 1 line #9407: document configparser.Error. ........ r83230 | georg.brandl | 2010-07-29 16:36:11 +0200 (Do, 29 Jul 2010) | 1 line Use correct directive and name. ........ r83232 | georg.brandl | 2010-07-29 16:49:08 +0200 (Do, 29 Jul 2010) | 1 line #9388: remove ERA_YEAR which is never defined in the source code. ........ ................ r83457 | georg.brandl | 2010-08-01 23:10:57 +0200 (So, 01 Aug 2010) | 9 lines Merged revisions 83223 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83223 | georg.brandl | 2010-07-29 15:38:37 +0200 (Do, 29 Jul 2010) | 1 line #3874: document HTMLParser.unknown_decl(). ........ ................ r83466 | georg.brandl | 2010-08-01 23:23:50 +0200 (So, 01 Aug 2010) | 29 lines Merged revisions 83160-83161,83166,83168-83169,83171 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83160 | georg.brandl | 2010-07-26 10:51:42 +0200 (Mo, 26 Jul 2010) | 1 line #9381: fix markup. ........ r83161 | georg.brandl | 2010-07-26 11:33:12 +0200 (Mo, 26 Jul 2010) | 1 line Add Brian Quinlan. ........ r83166 | georg.brandl | 2010-07-26 17:11:49 +0200 (Mo, 26 Jul 2010) | 1 line Fix grammar. ........ r83168 | georg.brandl | 2010-07-26 19:00:20 +0200 (Mo, 26 Jul 2010) | 1 line Fix indentation in example. ........ r83169 | georg.brandl | 2010-07-26 19:09:32 +0200 (Mo, 26 Jul 2010) | 1 line Add Reid. ........ r83171 | georg.brandl | 2010-07-26 23:12:13 +0200 (Mo, 26 Jul 2010) | 1 line Clarify. ........ ................ r83471 | georg.brandl | 2010-08-01 23:26:45 +0200 (So, 01 Aug 2010) | 9 lines Merged revisions 83106 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83106 | georg.brandl | 2010-07-23 18:55:26 +0200 (Fr, 23 Jul 2010) | 1 line Fix some markup glitches. ........ ................ r83475 | georg.brandl | 2010-08-01 23:28:47 +0200 (So, 01 Aug 2010) | 9 lines Merged revisions 82965 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r82965 | georg.brandl | 2010-07-19 13:28:05 +0200 (Mo, 19 Jul 2010) | 1 line Clarification. Yay importlib! ........ ................ r83480 | georg.brandl | 2010-08-01 23:33:42 +0200 (So, 01 Aug 2010) | 17 lines Merged revisions 82871,82960-82961 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r82871 | georg.brandl | 2010-07-14 10:00:22 +0200 (Mi, 14 Jul 2010) | 1 line #9258: fix copy-paste errors. ........ r82960 | georg.brandl | 2010-07-19 08:52:35 +0200 (Mo, 19 Jul 2010) | 1 line Clarify. ........ r82961 | georg.brandl | 2010-07-19 08:57:52 +0200 (Mo, 19 Jul 2010) | 1 line Clarify :option: description. ........ ................ r83486 | georg.brandl | 2010-08-01 23:44:38 +0200 (So, 01 Aug 2010) | 13 lines Merged revisions 82832,82834 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r82832 | georg.brandl | 2010-07-12 11:00:29 +0200 (Mo, 12 Jul 2010) | 1 line Take care of duplicate target name warnings. ........ r82834 | georg.brandl | 2010-07-12 11:06:13 +0200 (Mo, 12 Jul 2010) | 1 line Use raw string literals for regexes containing backlash. ........ ................
197 lines
7.2 KiB
ReStructuredText
197 lines
7.2 KiB
ReStructuredText
|
|
:mod:`HTMLParser` --- Simple HTML and XHTML parser
|
|
==================================================
|
|
|
|
.. module:: HTMLParser
|
|
:synopsis: A simple parser that can handle HTML and XHTML.
|
|
|
|
.. note::
|
|
|
|
The :mod:`HTMLParser` module has been renamed to :mod:`html.parser` in Python
|
|
3.0. The :term:`2to3` tool will automatically adapt imports when converting
|
|
your sources to 3.0.
|
|
|
|
|
|
.. versionadded:: 2.2
|
|
|
|
.. index::
|
|
single: HTML
|
|
single: XHTML
|
|
|
|
This module defines a class :class:`HTMLParser` which serves as the basis for
|
|
parsing text files formatted in HTML (HyperText Mark-up Language) and XHTML.
|
|
Unlike the parser in :mod:`htmllib`, this parser is not based on the SGML parser
|
|
in :mod:`sgmllib`.
|
|
|
|
|
|
.. class:: HTMLParser()
|
|
|
|
The :class:`HTMLParser` class is instantiated without arguments.
|
|
|
|
An :class:`HTMLParser` instance is fed HTML data and calls handler functions when tags
|
|
begin and end. The :class:`HTMLParser` class is meant to be overridden by the
|
|
user to provide a desired behavior.
|
|
|
|
Unlike the parser in :mod:`htmllib`, this parser does not check that end tags
|
|
match start tags or call the end-tag handler for elements which are closed
|
|
implicitly by closing an outer element.
|
|
|
|
An exception is defined as well:
|
|
|
|
|
|
.. exception:: HTMLParseError
|
|
|
|
Exception raised by the :class:`HTMLParser` class when it encounters an error
|
|
while parsing. This exception provides three attributes: :attr:`msg` is a brief
|
|
message explaining the error, :attr:`lineno` is the number of the line on which
|
|
the broken construct was detected, and :attr:`offset` is the number of
|
|
characters into the line at which the construct starts.
|
|
|
|
:class:`HTMLParser` instances have the following methods:
|
|
|
|
|
|
.. method:: HTMLParser.reset()
|
|
|
|
Reset the instance. Loses all unprocessed data. This is called implicitly at
|
|
instantiation time.
|
|
|
|
|
|
.. method:: HTMLParser.feed(data)
|
|
|
|
Feed some text to the parser. It is processed insofar as it consists of
|
|
complete elements; incomplete data is buffered until more data is fed or
|
|
:meth:`close` is called.
|
|
|
|
|
|
.. method:: HTMLParser.close()
|
|
|
|
Force processing of all buffered data as if it were followed by an end-of-file
|
|
mark. This method may be redefined by a derived class to define additional
|
|
processing at the end of the input, but the redefined version should always call
|
|
the :class:`HTMLParser` base class method :meth:`close`.
|
|
|
|
|
|
.. method:: HTMLParser.getpos()
|
|
|
|
Return current line number and offset.
|
|
|
|
|
|
.. method:: HTMLParser.get_starttag_text()
|
|
|
|
Return the text of the most recently opened start tag. This should not normally
|
|
be needed for structured processing, but may be useful in dealing with HTML "as
|
|
deployed" or for re-generating input with minimal changes (whitespace between
|
|
attributes can be preserved, etc.).
|
|
|
|
|
|
.. method:: HTMLParser.handle_starttag(tag, attrs)
|
|
|
|
This method is called to handle the start of a tag. It is intended to be
|
|
overridden by a derived class; the base class implementation does nothing.
|
|
|
|
The *tag* argument is the name of the tag converted to lower case. The *attrs*
|
|
argument is a list of ``(name, value)`` pairs containing the attributes found
|
|
inside the tag's ``<>`` brackets. The *name* will be translated to lower case,
|
|
and quotes in the *value* have been removed, and character and entity references
|
|
have been replaced. For instance, for the tag ``<A
|
|
HREF="http://www.cwi.nl/">``, this method would be called as
|
|
``handle_starttag('a', [('href', 'http://www.cwi.nl/')])``.
|
|
|
|
.. versionchanged:: 2.6
|
|
All entity references from :mod:`htmlentitydefs` are now replaced in the attribute
|
|
values.
|
|
|
|
|
|
.. method:: HTMLParser.handle_startendtag(tag, attrs)
|
|
|
|
Similar to :meth:`handle_starttag`, but called when the parser encounters an
|
|
XHTML-style empty tag (``<a .../>``). This method may be overridden by
|
|
subclasses which require this particular lexical information; the default
|
|
implementation simple calls :meth:`handle_starttag` and :meth:`handle_endtag`.
|
|
|
|
|
|
.. method:: HTMLParser.handle_endtag(tag)
|
|
|
|
This method is called to handle the end tag of an element. It is intended to be
|
|
overridden by a derived class; the base class implementation does nothing. The
|
|
*tag* argument is the name of the tag converted to lower case.
|
|
|
|
|
|
.. method:: HTMLParser.handle_data(data)
|
|
|
|
This method is called to process arbitrary data. It is intended to be
|
|
overridden by a derived class; the base class implementation does nothing.
|
|
|
|
|
|
.. method:: HTMLParser.handle_charref(name)
|
|
|
|
This method is called to process a character reference of the form ``&#ref;``.
|
|
It is intended to be overridden by a derived class; the base class
|
|
implementation does nothing.
|
|
|
|
|
|
.. method:: HTMLParser.handle_entityref(name)
|
|
|
|
This method is called to process a general entity reference of the form
|
|
``&name;`` where *name* is an general entity reference. It is intended to be
|
|
overridden by a derived class; the base class implementation does nothing.
|
|
|
|
|
|
.. method:: HTMLParser.handle_comment(data)
|
|
|
|
This method is called when a comment is encountered. The *comment* argument is
|
|
a string containing the text between the ``--`` and ``--`` delimiters, but not
|
|
the delimiters themselves. For example, the comment ``<!--text-->`` will cause
|
|
this method to be called with the argument ``'text'``. It is intended to be
|
|
overridden by a derived class; the base class implementation does nothing.
|
|
|
|
|
|
.. method:: HTMLParser.handle_decl(decl)
|
|
|
|
Method called when an SGML ``doctype`` declaration is read by the parser.
|
|
The *decl* parameter will be the entire contents of the declaration inside
|
|
the ``<!...>`` markup. It is intended to be overridden by a derived class;
|
|
the base class implementation does nothing.
|
|
|
|
|
|
.. method:: HTMLParser.unknown_decl(data)
|
|
|
|
Method called when an unrecognized SGML declaration is read by the parser.
|
|
The *data* parameter will be the entire contents of the declaration inside
|
|
the ``<!...>`` markup. It is sometimes useful to be be overridden by a
|
|
derived class; the base class implementation throws an :exc:`HTMLParseError`.
|
|
|
|
|
|
.. method:: HTMLParser.handle_pi(data)
|
|
|
|
Method called when a processing instruction is encountered. The *data*
|
|
parameter will contain the entire processing instruction. For example, for the
|
|
processing instruction ``<?proc color='red'>``, this method would be called as
|
|
``handle_pi("proc color='red'")``. It is intended to be overridden by a derived
|
|
class; the base class implementation does nothing.
|
|
|
|
.. note::
|
|
|
|
The :class:`HTMLParser` class uses the SGML syntactic rules for processing
|
|
instructions. An XHTML processing instruction using the trailing ``'?'`` will
|
|
cause the ``'?'`` to be included in *data*.
|
|
|
|
|
|
.. _htmlparser-example:
|
|
|
|
Example HTML Parser Application
|
|
-------------------------------
|
|
|
|
As a basic example, below is a very basic HTML parser that uses the
|
|
:class:`HTMLParser` class to print out tags as they are encountered::
|
|
|
|
from HTMLParser import HTMLParser
|
|
|
|
class MyHTMLParser(HTMLParser):
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
print "Encountered the beginning of a %s tag" % tag
|
|
|
|
def handle_endtag(self, tag):
|
|
print "Encountered the end of a %s tag" % tag
|
|
|