ubuntu,debian,redhat,fedora,centos
« »
2008-10-25Python

125

一个Python HTMLParser的使用例子

#!/usr/bin/env python

import sys
import urllib
import HTMLParser

class 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, data

if len(sys.argv) > 1:
params = urllib.urlencode({‘ip’: sys.argv[1], ‘action’: 2})
else:
params = None

content = unicode(urllib.urlopen(‘http://www.ip138.com/ips8.asp’,params).read(), ‘GB2312′)

parser = CustomParser()
parser.feed(content)
parser.close()

您还可能感兴趣的内容

日志信息 »

该日志于2008-10-25 11:39由 admin 发表在Python分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »

返回顶部