From: Ezio Melotti Date: Fri, 28 Oct 2011 11:14:34 +0000 (+0300) Subject: Improve HTMLParser example in the doc. X-Git-Tag: v2.7.3rc1~366 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f9cc80d6018bfb5797c938afc7a17352489d82c2;p=python Improve HTMLParser example in the doc. --- diff --git a/Doc/library/htmlparser.rst b/Doc/library/htmlparser.rst index 0cdc7ca1a2..787ab4e386 100644 --- a/Doc/library/htmlparser.rst +++ b/Doc/library/htmlparser.rst @@ -186,16 +186,21 @@ An exception is defined as well: 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:: +As a basic example, below is a simple HTML parser that uses the +:class:`HTMLParser` class to print out start tags, end tags and data +as they are encountered:: from HTMLParser import HTMLParser class MyHTMLParser(HTMLParser): - def handle_starttag(self, tag, attrs): - print "Encountered the beginning of a %s tag" % tag - + print "Encountered a start tag:", tag def handle_endtag(self, tag): - print "Encountered the end of a %s tag" % tag + print "Encountered an end tag:", tag + def handle_data(self, data): + print "Encountered some data:", data + + parser = MyHTMLParser() + parser.feed('Test' + '

Parse me!

')