[Python-checkins] cpython (2.7): Improve HTMLParser example in the doc.
ezio.melotti
python-checkins at python.org
Fri Oct 28 13:36:23 CEST 2011
http://hg.python.org/cpython/rev/07a46bb5ee3c
changeset: 73161:07a46bb5ee3c
branch: 2.7
parent: 73155:3e72de3c8ad5
user: Ezio Melotti <ezio.melotti at gmail.com>
date: Fri Oct 28 14:14:34 2011 +0300
summary:
Improve HTMLParser example in the doc.
files:
Doc/library/htmlparser.rst | 19 ++++++++++++-------
1 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/Doc/library/htmlparser.rst b/Doc/library/htmlparser.rst
--- a/Doc/library/htmlparser.rst
+++ b/Doc/library/htmlparser.rst
@@ -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 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_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
-
+ parser = MyHTMLParser()
+ parser.feed('<html><head><title>Test</title></head>'
+ '<body><h1>Parse me!</h1></body></html>')
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list