def dateRange(start, end, step=1, format="%Y-%m-%d"):
strptime, strftime = datetime.datetime.strptime, datetime.datetime.strftime
days = (strptime(end, format) - strptime(start, format)).days
return [strftime(strptime(start, format) + datetime.timedelta(i), format) for i in xrange(0, days, step)]
ef weekend():
try:
dayday = dateRange(st, ed)
for day in dayday:
d =day.replace('-','')
date = d
server_url = "http://www.easybots.cn/api/holiday.php?d="
vop_url_request = urllib2.Request(server_url + date)
vop_response = urllib2.urlopen(vop_url_request)
vop_data = json.loads(vop_response.read())
if vop_data[date] == '1' or vop_data[date] == '2':
dayday.remove(day)
return dayday
except:
dayday = dateRange(st, ed)
return dayday
有这样一个weekend函数,去请求一些内容,但是每次执行出来会缺失一些内容怀疑是网速的问题,该怎么限制遍历时的频率或者几秒内访问一次呢?
typecho2017-06-14 10:55:10
在每次遍历后, 加个睡眠时间咯
time.sleep(1) # 睡眠1秒
也就是在你的代码可以调整为:
for day in dayday:
...(访问处理代码)
time.sleep(1)