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)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。