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)