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操作会使列表长度发生变化,从而导致列表错位导致,最后出来的结果不是想要的结果