#小练习 HTMLParser 分类: python 小练习 python 2013-06-03 15:54 162人阅读 评论(0) 收藏
from HTMLParser import HTMLParser
print 'Encounter some data:',data
p=myHTMLParser()
p.feed('<html><head><title>Test</title></head>'
class myHTMLParser(HTMLParser):
'''......HTMLParser.......'''
print 'Encounter a start tag:',tag
print 'Encounter a end tag:',tag
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