From: Ezio Melotti Date: Sat, 27 Jun 2009 22:58:15 +0000 (+0000) Subject: Updated the last example as requested in #6350 X-Git-Tag: v3.2a1~2986 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2fad00c1989edcaff9cc11b48153a33700a5432b;p=thirdparty%2FPython%2Fcpython.git Updated the last example as requested in #6350 --- diff --git a/Doc/library/html.parser.rst b/Doc/library/html.parser.rst index 78b76770f3b9..ef0ae83acfc3 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 = """

Title

I'm a paragraph!

""" + >>> + >>> 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