Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f99e4b5

Browse files
committed
Improve HTMLParser example in the doc and fix a couple minor things.
1 parent f50ffa9 commit f99e4b5

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

Doc/library/html.parser.rst

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ An exception is defined as well:
101101
.. method:: HTMLParser.handle_startendtag(tag, attrs)
102102

103103
Similar to :meth:`handle_starttag`, but called when the parser encounters an
104-
XHTML-style empty tag (``<a .../>``). This method may be overridden by
104+
XHTML-style empty tag (``<img ... />``). This method may be overridden by
105105
subclasses which require this particular lexical information; the default
106-
implementation simple calls :meth:`handle_starttag` and :meth:`handle_endtag`.
106+
implementation simply calls :meth:`handle_starttag` and :meth:`handle_endtag`.
107107

108108

109109
.. method:: HTMLParser.handle_endtag(tag)
@@ -178,27 +178,23 @@ An exception is defined as well:
178178
Example HTML Parser Application
179179
-------------------------------
180180

181-
As a basic example, below is a very basic HTML parser that uses the
182-
:class:`HTMLParser` class to print out tags as they are encountered::
183-
184-
>>> from html.parser import HTMLParser
185-
>>>
186-
>>> class MyHTMLParser(HTMLParser):
187-
... def handle_starttag(self, tag, attrs):
188-
... print("Encountered a {} start tag".format(tag))
189-
... def handle_endtag(self, tag):
190-
... print("Encountered a {} end tag".format(tag))
191-
...
192-
>>> page = """<html><h1>Title</h1><p>I'm a paragraph!</p></html>"""
193-
>>>
194-
>>> myparser = MyHTMLParser()
195-
>>> myparser.feed(page)
196-
Encountered a html start tag
197-
Encountered a h1 start tag
198-
Encountered a h1 end tag
199-
Encountered a p start tag
200-
Encountered a p end tag
201-
Encountered a html end tag
181+
As a basic example, below is a simple HTML parser that uses the
182+
:class:`HTMLParser` class to print out start tags, end tags, and data
183+
as they are encountered::
184+
185+
from html.parser import HTMLParser
186+
187+
class MyHTMLParser(HTMLParser):
188+
def handle_starttag(self, tag, attrs):
189+
print("Encountered a start tag:", tag)
190+
def handle_endtag(self, tag):
191+
print("Encountered an end tag:", tag)
192+
def handle_data(self, data):
193+
print("Encountered some data:", data)
194+
195+
parser = MyHTMLParser()
196+
parser.feed('<html><head><title>Test</title></head>'
197+
'<body><h1>Parse me!</h1></body></html>')
202198

203199

204200
.. rubric:: Footnotes

0 commit comments

Comments
 (0)