13.1.1 Example HTML Parser

As a basic example, below is a very basic HTML parser that uses the HTMLParser class to print out tags 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

    def handle_endtag(self, tag):
        print "Encountered the end of a %s tag" % tag
See About this document... for information on suggesting changes.