Mercurial > cpython-lru_cache-weakref
annotate Doc/library/html.parser.rst @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 569566cbfd13 |
| children | 8dd2f5754b2f |
| rev | line source |
|---|---|
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
1 :mod:`html.parser` --- Simple HTML and XHTML parser |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
2 =================================================== |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
3 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
4 .. module:: html.parser |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
5 :synopsis: A simple parser that can handle HTML and XHTML. |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
6 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
7 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
8 .. index:: |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
9 single: HTML |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
10 single: XHTML |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
11 |
| 67779 | 12 **Source code:** :source:`Lib/html/parser.py` |
| 13 | |
| 14 -------------- | |
| 15 | |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
16 This module defines a class :class:`HTMLParser` which serves as the basis for |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
17 parsing text files formatted in HTML (HyperText Mark-up Language) and XHTML. |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
18 |
|
66623
1cbfeffea19f
#1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents:
63674
diff
changeset
|
19 .. class:: HTMLParser(strict=True) |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
20 |
|
66623
1cbfeffea19f
#1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents:
63674
diff
changeset
|
21 Create a parser instance. If *strict* is ``True`` (the default), invalid |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
22 HTML results in :exc:`~html.parser.HTMLParseError` exceptions [#]_. If |
|
66623
1cbfeffea19f
#1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents:
63674
diff
changeset
|
23 *strict* is ``False``, the parser uses heuristics to make a best guess at |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
24 the intention of any invalid HTML it encounters, similar to the way most |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
25 browsers do. Using ``strict=False`` is advised. |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
26 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
27 An :class:`.HTMLParser` instance is fed HTML data and calls handler methods |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
28 when start tags, end tags, text, comments, and other markup elements are |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
29 encountered. The user should subclass :class:`.HTMLParser` and override its |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
30 methods to implement the desired behavior. |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
31 |
|
47434
f150177ff1ff
Remove the htmllib and sgmllib modules as per PEP 3108.
Georg Brandl <georg@python.org>
parents:
47151
diff
changeset
|
32 This parser does not check that end tags match start tags or call the end-tag |
|
f150177ff1ff
Remove the htmllib and sgmllib modules as per PEP 3108.
Georg Brandl <georg@python.org>
parents:
47151
diff
changeset
|
33 handler for elements which are closed implicitly by closing an outer element. |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
34 |
|
66624
6e49af2e0891
Add missing versionchanged, correct 'throw' wording to 'raise'.
R. David Murray <rdmurray@bitdance.com>
parents:
66623
diff
changeset
|
35 .. versionchanged:: 3.2 *strict* keyword added |
|
6e49af2e0891
Add missing versionchanged, correct 'throw' wording to 'raise'.
R. David Murray <rdmurray@bitdance.com>
parents:
66623
diff
changeset
|
36 |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
37 An exception is defined as well: |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
38 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
39 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
40 .. exception:: HTMLParseError |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
41 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
42 Exception raised by the :class:`HTMLParser` class when it encounters an error |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
43 while parsing and *strict* is ``True``. This exception provides three |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
44 attributes: :attr:`msg` is a brief message explaining the error, |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
45 :attr:`lineno` is the number of the line on which the broken construct was |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
46 detected, and :attr:`offset` is the number of characters into the line at |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
47 which the construct starts. |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
48 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
49 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
50 Example HTML Parser Application |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
51 ------------------------------- |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
52 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
53 As a basic example, below is a simple HTML parser that uses the |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
54 :class:`HTMLParser` class to print out start tags, end tags, and data |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
55 as they are encountered:: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
56 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
57 from html.parser import HTMLParser |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
58 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
59 class MyHTMLParser(HTMLParser): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
60 def handle_starttag(self, tag, attrs): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
61 print("Encountered a start tag:", tag) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
62 def handle_endtag(self, tag): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
63 print("Encountered an end tag :", tag) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
64 def handle_data(self, data): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
65 print("Encountered some data :", data) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
66 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
67 parser = MyHTMLParser(strict=False) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
68 parser.feed('<html><head><title>Test</title></head>' |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
69 '<body><h1>Parse me!</h1></body></html>') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
70 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
71 The output will then be:: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
72 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
73 Encountered a start tag: html |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
74 Encountered a start tag: head |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
75 Encountered a start tag: title |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
76 Encountered some data : Test |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
77 Encountered an end tag : title |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
78 Encountered an end tag : head |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
79 Encountered a start tag: body |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
80 Encountered a start tag: h1 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
81 Encountered some data : Parse me! |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
82 Encountered an end tag : h1 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
83 Encountered an end tag : body |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
84 Encountered an end tag : html |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
85 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
86 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
87 :class:`.HTMLParser` Methods |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
88 ---------------------------- |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
89 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
90 :class:`HTMLParser` instances have the following methods: |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
91 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
92 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
93 .. method:: HTMLParser.feed(data) |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
94 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
95 Feed some text to the parser. It is processed insofar as it consists of |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
96 complete elements; incomplete data is buffered until more data is fed or |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
97 :meth:`close` is called. *data* must be :class:`str`. |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
98 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
99 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
100 .. method:: HTMLParser.close() |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
101 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
102 Force processing of all buffered data as if it were followed by an end-of-file |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
103 mark. This method may be redefined by a derived class to define additional |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
104 processing at the end of the input, but the redefined version should always call |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
105 the :class:`HTMLParser` base class method :meth:`close`. |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
106 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
107 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
108 .. method:: HTMLParser.reset() |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
109 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
110 Reset the instance. Loses all unprocessed data. This is called implicitly at |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
111 instantiation time. |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
112 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
113 |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
114 .. method:: HTMLParser.getpos() |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
115 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
116 Return current line number and offset. |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
117 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
118 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
119 .. method:: HTMLParser.get_starttag_text() |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
120 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
121 Return the text of the most recently opened start tag. This should not normally |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
122 be needed for structured processing, but may be useful in dealing with HTML "as |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
123 deployed" or for re-generating input with minimal changes (whitespace between |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
124 attributes can be preserved, etc.). |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
125 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
126 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
127 The following methods are called when data or markup elements are encountered |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
128 and they are meant to be overridden in a subclass. The base class |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
129 implementations do nothing (except for :meth:`~HTMLParser.handle_startendtag`): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
130 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
131 |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
132 .. method:: HTMLParser.handle_starttag(tag, attrs) |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
133 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
134 This method is called to handle the start of a tag (e.g. ``<div id="main">``). |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
135 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
136 The *tag* argument is the name of the tag converted to lower case. The *attrs* |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
137 argument is a list of ``(name, value)`` pairs containing the attributes found |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
138 inside the tag's ``<>`` brackets. The *name* will be translated to lower case, |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
139 and quotes in the *value* have been removed, and character and entity references |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
140 have been replaced. |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
141 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
142 For instance, for the tag ``<A HREF="http://www.cwi.nl/">``, this method |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
143 would be called as ``handle_starttag('a', [('href', 'http://www.cwi.nl/')])``. |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
144 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
145 All entity references from :mod:`html.entities` are replaced in the attribute |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
146 values. |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
147 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
148 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
149 .. method:: HTMLParser.handle_endtag(tag) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
150 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
151 This method is called to handle the end tag of an element (e.g. ``</div>``). |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
152 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
153 The *tag* argument is the name of the tag converted to lower case. |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
154 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
155 |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
156 .. method:: HTMLParser.handle_startendtag(tag, attrs) |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
157 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
158 Similar to :meth:`handle_starttag`, but called when the parser encounters an |
|
73163
9ddcb6d44bb4
Improve HTMLParser example in the doc and fix a couple minor things.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68798
diff
changeset
|
159 XHTML-style empty tag (``<img ... />``). This method may be overridden by |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
160 subclasses which require this particular lexical information; the default |
|
73163
9ddcb6d44bb4
Improve HTMLParser example in the doc and fix a couple minor things.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68798
diff
changeset
|
161 implementation simply calls :meth:`handle_starttag` and :meth:`handle_endtag`. |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
162 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
163 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
164 .. method:: HTMLParser.handle_data(data) |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
165 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
166 This method is called to process arbitrary data (e.g. text nodes and the |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
167 content of ``<script>...</script>`` and ``<style>...</style>``). |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
168 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
169 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
170 .. method:: HTMLParser.handle_entityref(name) |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
171 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
172 This method is called to process a named character reference of the form |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
173 ``&name;`` (e.g. ``>``), where *name* is a general entity reference |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
174 (e.g. ``'gt'``). |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
175 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
176 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
177 .. method:: HTMLParser.handle_charref(name) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
178 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
179 This method is called to process decimal and hexadecimal numeric character |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
180 references of the form ``&#NNN;`` and ``&#xNNN;``. For example, the decimal |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
181 equivalent for ``>`` is ``>``, whereas the hexadecimal is ``>``; |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
182 in this case the method will receive ``'62'`` or ``'x3E'``. |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
183 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
184 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
185 .. method:: HTMLParser.handle_comment(data) |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
186 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
187 This method is called when a comment is encountered (e.g. ``<!--comment-->``). |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
188 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
189 For example, the comment ``<!-- comment -->`` will cause this method to be |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
190 called with the argument ``' comment '``. |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
191 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
192 The content of Internet Explorer conditional comments (condcoms) will also be |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
193 sent to this method, so, for ``<!--[if IE 9]>IE9-specific content<![endif]-->``, |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
194 this method will receive ``'[if IE 9]>IE-specific content<![endif]'``. |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
195 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
196 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
197 .. method:: HTMLParser.handle_decl(decl) |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
198 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
199 This method is called to handle an HTML doctype declaration (e.g. |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
200 ``<!DOCTYPE html>``). |
|
63284
054f3279e3cc
#3874: document HTMLParser.unknown_decl().
Georg Brandl <georg@python.org>
parents:
55600
diff
changeset
|
201 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
202 The *decl* parameter will be the entire contents of the declaration inside |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
203 the ``<!...>`` markup (e.g. ``'DOCTYPE html'``). |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
204 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
205 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
206 .. method:: HTMLParser.handle_pi(data) |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
207 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
208 Method called when a processing instruction is encountered. The *data* |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
209 parameter will contain the entire processing instruction. For example, for the |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
210 processing instruction ``<?proc color='red'>``, this method would be called as |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
211 ``handle_pi("proc color='red'")``. It is intended to be overridden by a derived |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
212 class; the base class implementation does nothing. |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
213 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
214 .. note:: |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
215 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
216 The :class:`HTMLParser` class uses the SGML syntactic rules for processing |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
217 instructions. An XHTML processing instruction using the trailing ``'?'`` will |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
218 cause the ``'?'`` to be included in *data*. |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
219 |
|
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
220 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
221 .. method:: HTMLParser.unknown_decl(data) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
222 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
223 This method is called when an unrecognized declaration is read by the parser. |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
224 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
225 The *data* parameter will be the entire contents of the declaration inside |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
226 the ``<![...]>`` markup. It is sometimes useful to be overridden by a |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
227 derived class. The base class implementation raises an :exc:`HTMLParseError` |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
228 when *strict* is ``True``. |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
229 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
230 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
231 .. _htmlparser-examples: |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
232 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
233 Examples |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
234 -------- |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
235 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
236 The following class implements a parser that will be used to illustrate more |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
237 examples:: |
|
73163
9ddcb6d44bb4
Improve HTMLParser example in the doc and fix a couple minor things.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68798
diff
changeset
|
238 |
|
9ddcb6d44bb4
Improve HTMLParser example in the doc and fix a couple minor things.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68798
diff
changeset
|
239 from html.parser import HTMLParser |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
240 from html.entities import name2codepoint |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
241 |
|
73163
9ddcb6d44bb4
Improve HTMLParser example in the doc and fix a couple minor things.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68798
diff
changeset
|
242 class MyHTMLParser(HTMLParser): |
|
9ddcb6d44bb4
Improve HTMLParser example in the doc and fix a couple minor things.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68798
diff
changeset
|
243 def handle_starttag(self, tag, attrs): |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
244 print("Start tag:", tag) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
245 for attr in attrs: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
246 print(" attr:", attr) |
|
73163
9ddcb6d44bb4
Improve HTMLParser example in the doc and fix a couple minor things.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68798
diff
changeset
|
247 def handle_endtag(self, tag): |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
248 print("End tag :", tag) |
|
73163
9ddcb6d44bb4
Improve HTMLParser example in the doc and fix a couple minor things.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68798
diff
changeset
|
249 def handle_data(self, data): |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
250 print("Data :", data) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
251 def handle_comment(self, data): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
252 print("Comment :", data) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
253 def handle_entityref(self, name): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
254 c = chr(name2codepoint[name]) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
255 print("Named ent:", c) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
256 def handle_charref(self, name): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
257 if name.startswith('x'): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
258 c = chr(int(name[1:], 16)) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
259 else: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
260 c = chr(int(name)) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
261 print("Num ent :", c) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
262 def handle_decl(self, data): |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
263 print("Decl :", data) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
264 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
265 parser = MyHTMLParser(strict=False) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
266 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
267 Parsing a doctype:: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
268 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
269 >>> parser.feed('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ' |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
270 ... '"http://www.w3.org/TR/html4/strict.dtd">') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
271 Decl : DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
272 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
273 Parsing an element with a few attributes and a title:: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
274 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
275 >>> parser.feed('<img src="python-logo.png" alt="The Python logo">') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
276 Start tag: img |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
277 attr: ('src', 'python-logo.png') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
278 attr: ('alt', 'The Python logo') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
279 >>> |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
280 >>> parser.feed('<h1>Python</h1>') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
281 Start tag: h1 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
282 Data : Python |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
283 End tag : h1 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
284 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
285 The content of ``script`` and ``style`` elements is returned as is, without |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
286 further parsing:: |
|
73163
9ddcb6d44bb4
Improve HTMLParser example in the doc and fix a couple minor things.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68798
diff
changeset
|
287 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
288 >>> parser.feed('<style type="text/css">#python { color: green }</style>') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
289 Start tag: style |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
290 attr: ('type', 'text/css') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
291 Data : #python { color: green } |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
292 End tag : style |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
293 >>> |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
294 >>> parser.feed('<script type="text/javascript">' |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
295 ... 'alert("<strong>hello!</strong>");</script>') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
296 Start tag: script |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
297 attr: ('type', 'text/javascript') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
298 Data : alert("<strong>hello!</strong>"); |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
299 End tag : script |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
300 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
301 Parsing comments:: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
302 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
303 >>> parser.feed('<!-- a comment -->' |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
304 ... '<!--[if IE 9]>IE-specific content<![endif]-->') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
305 Comment : a comment |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
306 Comment : [if IE 9]>IE-specific content<![endif] |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
307 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
308 Parsing named and numeric character references and converting them to the |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
309 correct char (note: these 3 references are all equivalent to ``'>'``):: |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
310 |
|
75010
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
311 >>> parser.feed('>>>') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
312 Named ent: > |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
313 Num ent : > |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
314 Num ent : > |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
315 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
316 Feeding incomplete chunks to :meth:`~HTMLParser.feed` works, but |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
317 :meth:`~HTMLParser.handle_data` might be called more than once:: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
318 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
319 >>> for chunk in ['<sp', 'an>buff', 'ered ', 'text</s', 'pan>']: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
320 ... parser.feed(chunk) |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
321 ... |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
322 Start tag: span |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
323 Data : buff |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
324 Data : ered |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
325 Data : text |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
326 End tag : span |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
327 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
328 Parsing invalid HTML (e.g. unquoted attributes) also works:: |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
329 |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
330 >>> parser.feed('<p><a class=link href=#main>tag soup</p ></a>') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
331 Start tag: p |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
332 Start tag: a |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
333 attr: ('class', 'link') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
334 attr: ('href', '#main') |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
335 Data : tag soup |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
336 End tag : p |
|
569566cbfd13
#14020: improve HTMLParser documentation.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73274
diff
changeset
|
337 End tag : a |
|
47151
cdb75af2385a
Merged revisions 63438 via svnmerge from
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
338 |
|
66623
1cbfeffea19f
#1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents:
63674
diff
changeset
|
339 .. rubric:: Footnotes |
|
1cbfeffea19f
#1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents:
63674
diff
changeset
|
340 |
|
66624
6e49af2e0891
Add missing versionchanged, correct 'throw' wording to 'raise'.
R. David Murray <rdmurray@bitdance.com>
parents:
66623
diff
changeset
|
341 .. [#] For backward compatibility reasons *strict* mode does not raise |
|
6e49af2e0891
Add missing versionchanged, correct 'throw' wording to 'raise'.
R. David Murray <rdmurray@bitdance.com>
parents:
66623
diff
changeset
|
342 exceptions for all non-compliant HTML. That is, some invalid HTML |
|
66623
1cbfeffea19f
#1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents:
63674
diff
changeset
|
343 is tolerated even in *strict* mode. |
