Chinaunix首页 | 论坛 | 博客
  • 博客访问: 134499
  • 博文数量: 73
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 760
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-29 14:07
文章分类
文章存档

2011年(2)

2010年(4)

2009年(40)

2008年(27)

我的朋友

分类: Python/Ruby

2009-11-11 16:38:50

原文
http://crquan.blogbus.com/logs/8269701.html

#!/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('',params).read(), 'GB2312')
 
parser = CustomParser()
parser.feed(content)
parser.close()
阅读(2543) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~