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


分类:
Python


698人阅读
评论(1)
收藏
举报
可以扩充成为简单的抓取工具,定时抓取

Python通过HTTP协议定期抓取文件#!usr/bin/python
Python通过HTTP协议定期抓取文件

Python通过HTTP协议定期抓取文件
importurllib2,time;
Python通过HTTP协议定期抓取文件
classErrorHandler(urllib2.HTTPDefaultErrorHandler):
Python通过HTTP协议定期抓取文件
defhttp_error_default(self,req,fp,code,msg,headers):
Python通过HTTP协议定期抓取文件result
=urllib2.HTTPError(req.get_full_url(),code,msg,headers,fp)
Python通过HTTP协议定期抓取文件result.status
=code
Python通过HTTP协议定期抓取文件
returnresult
Python通过HTTP协议定期抓取文件
Python通过HTTP协议定期抓取文件URL
='http://www.ibm.com/developerworks/js/ajax1.js'
Python通过HTTP协议定期抓取文件req
=urllib2.Request(URL)
Python通过HTTP协议定期抓取文件mgr
=urllib2.build_opener(ErrorHandler())
Python通过HTTP协议定期抓取文件
Python通过HTTP协议定期抓取文件
whileTrue:
Python通过HTTP协议定期抓取文件ns
=mgr.open(req)
Python通过HTTP协议定期抓取文件
if(ns.headers.has_key('last-modified')):
Python通过HTTP协议定期抓取文件modified
=ns.headers.get('last-modified')
Python通过HTTP协议定期抓取文件
if(ns.code==304):
Python通过HTTP协议定期抓取文件
print'''
Python通过HTTP协议定期抓取文件==============================
Python通过HTTP协议定期抓取文件NOTMODIFIED
Python通过HTTP协议定期抓取文件==============================
Python通过HTTP协议定期抓取文件
'''
Python通过HTTP协议定期抓取文件
elif(ns.code==200):
Python通过HTTP协议定期抓取文件
printns.read()
Python通过HTTP协议定期抓取文件
else:
Python通过HTTP协议定期抓取文件
print'thereisanerror';
Python通过HTTP协议定期抓取文件
Python通过HTTP协议定期抓取文件
if(notlocals().has_key('modified')):
Python通过HTTP协议定期抓取文件modified
=time.time();
Python通过HTTP协议定期抓取文件req.add_header(
'If-Modified-Since',modified)
Python通过HTTP协议定期抓取文件time.sleep(
10)

发表回复