#小练习 HTMLParser 分类: python 小练习 python 2013-06-03 15:54 162人阅读 评论(0) 收藏
2013-06-03 15:54
871 查看
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:
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解析网页,提取链接地址、标题名称,并插入数据库 分类: python 小练习 HTMLParser 2014-02-19 09:57 519人阅读 评论(0) 收藏
- #小练习 使用字典保存HTMLParser解析的数据 分类: python 小练习 HTMLParser 2013-11-08 20:51 574人阅读 评论(0) 收藏
- #小练习 解析HTML文件并使用字典保存链接 分类: HTMLParser python 小练习 2013-11-11 12:06 267人阅读 评论(0) 收藏
- #小练习 使用HTMLParser获取data时注意事项 分类: python 小练习 HTMLParser 2013-11-08 21:05 335人阅读 评论(0) 收藏
- SGMLParser (二) 分类: python 小练习 HTMLParser 2014-02-20 14:06 362人阅读 评论(0) 收藏
- #小练习 动态生成密码 分类: python 小练习 2013-08-15 16:25 314人阅读 评论(0) 收藏
- 使用glob模块,遍历E盘下的mp3文件 分类: python 小练习 2013-05-15 15:40 405人阅读 评论(0) 收藏
- 深拷贝与浅拷贝的练习 分类: python 小练习 2013-10-29 16:29 284人阅读 评论(0) 收藏
- Python之HTML的解析(网页抓取一) 分类: HTMLParser 2013-11-08 20:32 5203人阅读 评论(0) 收藏
- 使用locals()获得类,进行分发 分类: python 小练习 divide into python python基础学习 2014-02-21 14:51 217人阅读 评论(0) 收藏
- 使用StringIO 分类: python 小练习 2013-06-14 16:00 233人阅读 评论(0) 收藏
- python学习笔记十 类文件对象 分类: python基础学习 python 2013-06-18 12:03 254人阅读 评论(0) 收藏
- 输出文件中不包含指定的多个排除词的行内容(双重遍历) 分类: python 小练习 2013-11-22 15:20 605人阅读 评论(0) 收藏
- 删除列表元素时需注意的问题 分类: python基础学习 python 小练习 2013-10-09 14:02 293人阅读 评论(0) 收藏
- #小练习 合并首字母相同的男孩、女孩姓名 (最终版) 分类: python 小练习 2013-11-25 16:52 283人阅读 评论(0) 收藏
- #遍历E盘下的mp3文件 分类: python 小练习 python 2013-05-14 18:27 303人阅读 评论(0) 收藏
- 序列中对应列的值相加 分类: python 小练习 2013-12-05 19:09 293人阅读 评论(0) 收藏
- 输出n的因子 分类: python 小练习 2013-12-13 17:20 229人阅读 评论(0) 收藏
- #小练习 pickle模块_分别存储python对象 分类: python Module 2013-09-05 18:07 379人阅读 评论(0) 收藏
- 多态 分发 分类: python 小练习 divide into python 2013-11-05 19:11 394人阅读 评论(0) 收藏