Mercurial > cpython-lru_cache-weakref
diff Doc/library/htmlparser.rst @ 73161:07a46bb5ee3c 2.7
Improve HTMLParser example in the doc.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 28 Oct 2011 14:14:34 +0300 |
| parents | b5674229d0f9 |
| children | 9a6699a8aee9 |
line wrap: on
line diff
--- a/Doc/library/htmlparser.rst Fri Oct 28 12:22:25 2011 +0300 +++ b/Doc/library/htmlparser.rst Fri Oct 28 14:14:34 2011 +0300 @@ -186,16 +186,21 @@ 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 an end tag:", tag + def handle_data(self, data): + print "Encountered some data:", data - def handle_endtag(self, tag): - print "Encountered the end of a %s tag" % tag + parser = MyHTMLParser() + parser.feed('<html><head><title>Test</title></head>' + '<body><h1>Parse me!</h1></body></html>')
