summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2009-06-27 22:58:15 (GMT)
committerEzio Melotti <ezio.melotti@gmail.com>2009-06-27 22:58:15 (GMT)
commit2fad00c1989edcaff9cc11b48153a33700a5432b (patch)
tree988c27b188a622522dc99f11022791613241d65c
parent095386ef135c5dfcb3efc522c803da307e80fe34 (diff)
downloadcpython-2fad00c1989edcaff9cc11b48153a33700a5432b.zip
cpython-2fad00c1989edcaff9cc11b48153a33700a5432b.tar.gz
cpython-2fad00c1989edcaff9cc11b48153a33700a5432b.tar.bz2
Updated the last example as requested in #6350
-rw-r--r--Doc/library/html.parser.rst26
1 files changed, 18 insertions, 8 deletions
diff --git a/Doc/library/html.parser.rst b/Doc/library/html.parser.rst
index 78b7677..ef0ae83 100644
--- a/Doc/library/html.parser.rst
+++ b/Doc/library/html.parser.rst
@@ -163,13 +163,23 @@ 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 html.parser import HTMLParser
+ >>> from html.parser import HTMLParser
+ >>>
+ >>> class MyHTMLParser(HTMLParser):
+ ... def handle_starttag(self, tag, attrs):
+ ... print("Encountered a {} start tag".format(tag))
+ ... def handle_endtag(self, tag):
+ ... print("Encountered a {} end tag".format(tag))
+ ...
+ >>> page = """<html><h1>Title</h1><p>I'm a paragraph!</p></html>"""
+ >>>
+ >>> myparser = MyHTMLParser()
+ >>> myparser.feed(page)
+ Encountered a html start tag
+ Encountered a h1 start tag
+ Encountered a h1 end tag
+ Encountered a p start tag
+ Encountered a p end tag
+ Encountered a html end tag
- 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