#小练习 HTMLParser

from HTMLParser import HTMLParser

class myHTMLParser(HTMLParser):

    '''......HTMLParser.......'''

    def handle_starttag(self,tag,attrs):

        print 'Encounter a start tag:',tag


    def handle_endtag(self,tag):

        print  'Encounter a end tag:',tag


    def handle_data(self,data):
        print  'Encounter some data:',data


p=myHTMLParser()
p.feed('<html><head><title>Test</title></head>'

            '<body><h1>Parse me!</h1></body></html>')



The output will then be:

Encountered a start tag: html
Encountered a start tag: head
Encountered a start tag: title
Encountered some data  : Test
Encountered an end tag : title
Encountered an end tag : head
Encountered a start tag: body
Encountered a start tag: h1
Encountered some data  : Parse me!
Encountered an end tag : h1
Encountered an end tag : body
Encountered an end tag : html

你可能感兴趣的:(#小练习 HTMLParser)