2008-02-19
Python通过HTTP协议定期抓取文件(ZT)
#!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)
发表评论
- 浏览: 14355 次
- 性别:


- 详细资料
搜索本博客
最新评论
-
py2exe越来越爽了
[size=large][size=medium]大家好,python新手。现遇 ...
-- by davidsun2008 -
DLang策略模式
我会尝试着把模式详尽剖析。不过能力有限,望大家多提宝贵意见。
-- by jinheking -
DLang策略模式
不错的理念,赞一个。 能详尽剖析就更好了
-- by DavidL -
DLang策略模式
可以像很多D项目一样,自己写一个all.d,在这里面import上所有的modu ...
-- by shawind -
DLang策略模式
引用module 怎么让我的程序做的像import std.stdio; 而不用 ...
-- by jinheking






评论排行榜