#小练习 HTMLParser 分类: python 小练习 python 2013-06-03 15:54 162人阅读 评论(0) 收藏

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

posted @ 2013-06-03 15:54  前行者2011  阅读(122)  评论(0编辑  收藏  举报