python使用HTMLParser解析html,pythonhtmlparser,一个Python HTM
python使用HTMLParser解析html,pythonhtmlparser,一个Python HTM
一个Python HTMLParser的使用例子
#!/usr/bin/env pythonimport sysimport urllibimport HTMLParserclass CustomParser(HTMLParser.HTMLParser):selected = (‘table’, ‘h1′, ‘font’, ‘ul’, ‘li’, ‘tr’, ‘td’, ‘a’)def reset(self):HTMLParser.HTMLParser.reset(self)self._level_stack = []def handle_starttag(self, tag, attrs):if tag in CustomParser.selected:self._level_stack.append(tag)def handle_endtag(self, tag):if self._level_stack \and tag in CustomParser.selected \and tag == self._level_stack[-1]:self._level_stack.pop()def handle_data(self, data):if “/”.join(self._level_stack) in (‘table/tr/td’,‘table/tr/td/h1/font’,‘table/tr/td/ul/li’):print self._level_stack, dataif len(sys.argv) > 1:params = urllib.urlencode({‘ip’: sys.argv[1], ‘action’: 2})else:params = Nonecontent = unicode(urllib.urlopen(‘http://www.ip138.com/ips8.asp’,params).read(), ‘GB2312′)parser = CustomParser()parser.feed(content)parser.close()
相关内容
- Python list使用示例,pythonlist示例,#!/usr/bin/p
- python if 语句,pythonif,#!/usr/bin/p
- python while语句,pythonwhile,python while
- Python使用cx_Oracle将oracle数据表中的数据导出成csv,cx_
- python正则re模块学习笔记,pythonre学习笔记,python正则r
- Python 初学教程 for语句,python初学for语句,python for 语
- Python 处理mp3文件中的图片和歌词信息,,一些MP3播放器
- Python使用PyMedia播放mp3,wave等文件,pythonpymedia,Pymedia 是
- Python合并文本文件代码,,# coding gbk
- python 初学教程 做比较和逻辑运算,python初学,python 做比
评论关闭