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函數,去請求一些內容,但是每次執行出來會缺失一些內容懷疑是網速的問題,該怎麼限制遍歷時的頻率或者幾秒內訪問一次呢?
在每次遍歷後, 加個睡眠時間咯
也就是在你的程式碼可以調整為:
找到問題了,在遍歷的時候對原始列表進行remove操作會使列表長度發生變化,從而導致列表錯位導致,最後出來的結果不是想要的結果