/Doc/library/htmllib.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 195 lines · 126 code · 69 blank · 0 comment · 0 complexity · c5e8450c7cf0b13ffb0e7a6d11ce9cd7 MD5 · raw file

  1. :mod:`htmllib` --- A parser for HTML documents
  2. ==============================================
  3. .. module:: htmllib
  4. :synopsis: A parser for HTML documents.
  5. :deprecated:
  6. .. deprecated:: 2.6
  7. The :mod:`htmllib` module has been removed in Python 3.0.
  8. .. index::
  9. single: HTML
  10. single: hypertext
  11. .. index::
  12. module: sgmllib
  13. module: formatter
  14. single: SGMLParser (in module sgmllib)
  15. This module defines a class which can serve as a base for parsing text files
  16. formatted in the HyperText Mark-up Language (HTML). The class is not directly
  17. concerned with I/O --- it must be provided with input in string form via a
  18. method, and makes calls to methods of a "formatter" object in order to produce
  19. output. The :class:`HTMLParser` class is designed to be used as a base class
  20. for other classes in order to add functionality, and allows most of its methods
  21. to be extended or overridden. In turn, this class is derived from and extends
  22. the :class:`SGMLParser` class defined in module :mod:`sgmllib`. The
  23. :class:`HTMLParser` implementation supports the HTML 2.0 language as described
  24. in :rfc:`1866`. Two implementations of formatter objects are provided in the
  25. :mod:`formatter` module; refer to the documentation for that module for
  26. information on the formatter interface.
  27. The following is a summary of the interface defined by
  28. :class:`sgmllib.SGMLParser`:
  29. * The interface to feed data to an instance is through the :meth:`feed` method,
  30. which takes a string argument. This can be called with as little or as much
  31. text at a time as desired; ``p.feed(a); p.feed(b)`` has the same effect as
  32. ``p.feed(a+b)``. When the data contains complete HTML markup constructs, these
  33. are processed immediately; incomplete constructs are saved in a buffer. To
  34. force processing of all unprocessed data, call the :meth:`close` method.
  35. For example, to parse the entire contents of a file, use::
  36. parser.feed(open('myfile.html').read())
  37. parser.close()
  38. * The interface to define semantics for HTML tags is very simple: derive a class
  39. and define methods called :meth:`start_tag`, :meth:`end_tag`, or :meth:`do_tag`.
  40. The parser will call these at appropriate moments: :meth:`start_tag` or
  41. :meth:`do_tag` is called when an opening tag of the form ``<tag ...>`` is
  42. encountered; :meth:`end_tag` is called when a closing tag of the form ``<tag>``
  43. is encountered. If an opening tag requires a corresponding closing tag, like
  44. ``<H1>`` ... ``</H1>``, the class should define the :meth:`start_tag` method; if
  45. a tag requires no closing tag, like ``<P>``, the class should define the
  46. :meth:`do_tag` method.
  47. The module defines a parser class and an exception:
  48. .. class:: HTMLParser(formatter)
  49. This is the basic HTML parser class. It supports all entity names required by
  50. the XHTML 1.0 Recommendation (http://www.w3.org/TR/xhtml1). It also defines
  51. handlers for all HTML 2.0 and many HTML 3.0 and 3.2 elements.
  52. .. exception:: HTMLParseError
  53. Exception raised by the :class:`HTMLParser` class when it encounters an error
  54. while parsing.
  55. .. versionadded:: 2.4
  56. .. seealso::
  57. Module :mod:`formatter`
  58. Interface definition for transforming an abstract flow of formatting events into
  59. specific output events on writer objects.
  60. Module :mod:`HTMLParser`
  61. Alternate HTML parser that offers a slightly lower-level view of the input, but
  62. is designed to work with XHTML, and does not implement some of the SGML syntax
  63. not used in "HTML as deployed" and which isn't legal for XHTML.
  64. Module :mod:`htmlentitydefs`
  65. Definition of replacement text for XHTML 1.0 entities.
  66. Module :mod:`sgmllib`
  67. Base class for :class:`HTMLParser`.
  68. .. _html-parser-objects:
  69. HTMLParser Objects
  70. ------------------
  71. In addition to tag methods, the :class:`HTMLParser` class provides some
  72. additional methods and instance variables for use within tag methods.
  73. .. attribute:: HTMLParser.formatter
  74. This is the formatter instance associated with the parser.
  75. .. attribute:: HTMLParser.nofill
  76. Boolean flag which should be true when whitespace should not be collapsed, or
  77. false when it should be. In general, this should only be true when character
  78. data is to be treated as "preformatted" text, as within a ``<PRE>`` element.
  79. The default value is false. This affects the operation of :meth:`handle_data`
  80. and :meth:`save_end`.
  81. .. method:: HTMLParser.anchor_bgn(href, name, type)
  82. This method is called at the start of an anchor region. The arguments
  83. correspond to the attributes of the ``<A>`` tag with the same names. The
  84. default implementation maintains a list of hyperlinks (defined by the ``HREF``
  85. attribute for ``<A>`` tags) within the document. The list of hyperlinks is
  86. available as the data attribute :attr:`anchorlist`.
  87. .. method:: HTMLParser.anchor_end()
  88. This method is called at the end of an anchor region. The default
  89. implementation adds a textual footnote marker using an index into the list of
  90. hyperlinks created by :meth:`anchor_bgn`.
  91. .. method:: HTMLParser.handle_image(source, alt[, ismap[, align[, width[, height]]]])
  92. This method is called to handle images. The default implementation simply
  93. passes the *alt* value to the :meth:`handle_data` method.
  94. .. method:: HTMLParser.save_bgn()
  95. Begins saving character data in a buffer instead of sending it to the formatter
  96. object. Retrieve the stored data via :meth:`save_end`. Use of the
  97. :meth:`save_bgn` / :meth:`save_end` pair may not be nested.
  98. .. method:: HTMLParser.save_end()
  99. Ends buffering character data and returns all data saved since the preceding
  100. call to :meth:`save_bgn`. If the :attr:`nofill` flag is false, whitespace is
  101. collapsed to single spaces. A call to this method without a preceding call to
  102. :meth:`save_bgn` will raise a :exc:`TypeError` exception.
  103. :mod:`htmlentitydefs` --- Definitions of HTML general entities
  104. ==============================================================
  105. .. module:: htmlentitydefs
  106. :synopsis: Definitions of HTML general entities.
  107. .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
  108. .. note::
  109. The :mod:`htmlentitydefs` module has been renamed to :mod:`html.entities` in
  110. Python 3.0. The :term:`2to3` tool will automatically adapt imports when
  111. converting your sources to 3.0.
  112. This module defines three dictionaries, ``name2codepoint``, ``codepoint2name``,
  113. and ``entitydefs``. ``entitydefs`` is used by the :mod:`htmllib` module to
  114. provide the :attr:`entitydefs` member of the :class:`HTMLParser` class. The
  115. definition provided here contains all the entities defined by XHTML 1.0 that
  116. can be handled using simple textual substitution in the Latin-1 character set
  117. (ISO-8859-1).
  118. .. data:: entitydefs
  119. A dictionary mapping XHTML 1.0 entity definitions to their replacement text in
  120. ISO Latin-1.
  121. .. data:: name2codepoint
  122. A dictionary that maps HTML entity names to the Unicode codepoints.
  123. .. versionadded:: 2.3
  124. .. data:: codepoint2name
  125. A dictionary that maps Unicode codepoints to HTML entity names.
  126. .. versionadded:: 2.3