我需要从Kucoin API下载2019年的1分钟kline数据。它允许每个请求获得1500个数据点,所以我创建了一个函数来获得所需的块长度。问题是,对于某些块,我会得到一个错误。
KucoinAPIException(<响应[429]>(
其为";请求太多";错误根据我在网上看到的限制是10秒内打30个电话,我在10秒内打1个,所以不太确定为什么会出现这个错误?当我做time.sleep(0(时,我得到了一个预期的临时块。
如何在善待API的同时获取大量数据?代码中的错误在哪里
def get_ku_his(sym, tf, str_start, str_end):
start_ts = int(datetime.strptime(str_start, '%Y-%m-%d %H:%M').replace(tzinfo=timezone.utc).timestamp())
if str_end==0:
end_ts=0
else:
end_ts = int(datetime.strptime(str_end, '%Y-%m-%d %H:%M').replace(tzinfo=timezone.utc).timestamp())
data = kuclient.get_kline_data(sym, tf, start_ts, end_ts)
data = pd.DataFrame(data, columns = ['timestamp', 'open', 'close', 'high', 'low', 'transaction amount', 'volume'])
data['timestamp'] = data['timestamp'].astype(float)*1000
data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms')
data.set_index('timestamp', inplace=True)
data = data[["close","volume"]]
data.dropna(inplace=True)
data = data.astype(float)
data = data.sort_index()
return data
def get_ku_hp(sym, tf, str_start):
data = get_ku_his(sym, tf, str_start, 0)
temp_start = data.index[0].strftime('%Y-%m-%d %H:%M')
while str_start < temp_start:
try:
temp = get_ku_his(sym, tf, str_start, temp_start)
print("Downloaded part from %s to %s" %(str_start, temp_start))
temp_start = temp.index[0].strftime('%Y-%m-%d %H:%M')
data = data.append(temp)
time.sleep(10)
except Exception as e: print(repr(e))
data = data.sort_index()
print("%s Downloaded %s data for %s from %s to %s" % (datetime.today().strftime("%H:%M:%S"), tf, sym, data.index[0], data.index[-1]))
return data
test = get_ku_hp("AIOZ-USDT","1min","2019-01-05 22:00")
控制台消息:
*Downloaded part from 2019-01-05 22:00 to 2021-10-19 11:45
Downloaded part from 2019-01-05 22:00 to 2021-10-18 10:45
Downloaded part from 2019-01-05 22:00 to 2021-10-17 09:45
KucoinAPIException(<Response [429]>)
Downloaded part from 2019-01-05 22:00 to 2021-10-16 08:45
Downloaded part from 2019-01-05 22:00 to 2021-10-15 07:45
Downloaded part from 2019-01-05 22:00 to 2021-10-14 06:45
Downloaded part from 2019-01-05 22:00 to 2021-10-13 05:45
Downloaded part from 2019-01-05 22:00 to 2021-10-12 04:45
Downloaded part from 2019-01-05 22:00 to 2021-10-11 03:45
Downloaded part from 2019-01-05 22:00 to 2021-10-10 02:45
Downloaded part from 2019-01-05 22:00 to 2021-10-09 01:45
Downloaded part from 2019-01-05 22:00 to 2021-10-08 00:45
Downloaded part from 2019-01-05 22:00 to 2021-10-06 23:45
Downloaded part from 2019-01-05 22:00 to 2021-10-05 22:45
Downloaded part from 2019-01-05 22:00 to 2021-10-04 21:45
Downloaded part from 2019-01-05 22:00 to 2021-10-03 20:45
KucoinAPIException(<Response [429]>)
KucoinAPIException(<Response [429]>)
Downloaded part from 2019-01-05 22:00 to 2021-10-02 19:45
Downloaded part from 2019-01-05 22:00 to 2021-10-01 18:45
Downloaded part from 2019-01-05 22:00 to 2021-09-30 17:45*
要进行大量请求,请尝试使用Websocket,这里有一个带有文档的lib。或者,您可以使用VPN或代理更改请求IP。