当前位置: 首页 >> 程序设计 >> Python通过HTTP协议定期抓取文件
 

Python通过HTTP协议定期抓取文件

作者:      来源:http://blog.csdn.net/foyuan     发表时间:2007-07-31     浏览次数:      字号:    

可以扩充成为简单的抓取工具,定时抓取


#!usr/bin/python

import urllib2,time;
class ErrorHandler(urllib2.HTTPDefaultErrorHandler):
    
def http_error_default(self, req, fp, code, msg, headers):
        result 
= urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp)
        result.status 
= code
        
return result

URL
='http://www.ibm.com/developerworks/js/ajax1.js'
req
=urllib2.Request(URL)
mgr
=urllib2.build_opener(ErrorHandler())

while True:
    ns
=mgr.open(req)
    
if(ns.headers.has_key('last-modified')):
        modified
=ns.headers.get('last-modified')
    
if(ns.code==304):
        
print '''
          ==============================
              NOT MODIFIED
          ==============================
        
'''
    
elif(ns.code==200):
        
print ns.read()
    
else:
        
print 'there is an error';
        
    
if(not locals().has_key('modified')):
        modified
=time.time();
    req.add_header(
'If-Modified-Since',modified)
    time.sleep(
10)
 

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
很多网页的header中并没有last-modified这个头的!
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •