You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Bitso API获取加密货币历史价格时请求相同响应却不一致

问题描述

尝试通过Bitso API存储加密货币历史价格,但每次运行代码时,不同交易对(symbol)会随机出现无数据的情况,无固定规律——有时部分交易对能获取到数据,部分不行,无数据的交易对会被打印出来,求排查建议。

交易对列表
base_assets = ['aave_usd',  'ada_usd',  'algo_usd',  'ape_usd',  'ars_usd',
'atom_usd',  'avax_usd',  'axs_usd',  'bal_usd',  'bat_usd',
'bch_usd',  'brl_usd',  'btc_usd',  'btsoeur_usd',  'chz_usd',
'comp_usd',  'cop_usd',  'crv_usd',  'dai_usd',  'doge_usd',
'dot_usd',  'dydx_usd',  'enj_usd',  'eth_usd',  'eur_usd',
'fcbar_usd',  'ftm_usd',  'galag_usd',  'galat_usd',  'gnt_usd',
'grt_usd',  'ldo_usd',  'link_usd',  'lrc_usd',  'ltc_usd',
'mana_usd',  'matic_usd',  'mkr_usd',  'mxn_usd',  'near_usd',
'omg_usd',  'paxg_usd',  'pepe_usd',  'psg_usd',  'qnt_usd',
'sand_usd',  'shib_usd',  'snx_usd',  'sol_usd',  'steth_usd',
'sushi_usd',  'tigres_usd',  'trx_usd',  'tusd_usd',  'uni_usd',
'usd_usd',  'usdc_usd',  'usdt_usd',  'xlm_usd',  'xrp_usd',
'yfi_usd']
当前代码
import requests
import json
import time
import pandas as pd 

endpoint = 'https://api.bitso.com/v3/ohlc/?book='  # https://github.com/kalinbas/bitso-historical-data

prices_df = pd.DataFrame()

for symbol in base_assets:
    params = {'book': symbol,
             'time_bucket': 86400}

    response = requests.get(endpoint, params=params) 
    result = json.loads(response.text)
    
    try:
        data = result['payload']

        # Create a DataFrame from the data
        temp_df = pd.DataFrame(data)

        # Convert timestamps to readable format in the DataFrame
        temp_df['bucket_start_time'] = pd.to_datetime(temp_df['bucket_start_time'], unit='ms')
        temp_df['first_trade_time'] = pd.to_datetime(temp_df['first_trade_time'], unit='ms')
        temp_df['last_trade_time'] = pd.to_datetime(temp_df['last_trade_time'], unit='ms')
        temp_df['symbol'] = symbol
        prices_df = prices_df.append(temp_df, ignore_index=True)
    except:
        print(symbol)
排查建议
  • 输出详细错误信息:把空except改成捕获具体异常,同时打印响应状态码和内容,准确定位问题:
    except Exception as e:
        print(f"{symbol} 出错: {str(e)}")
        print(f"响应状态码: {response.status_code}")
        print(f"响应内容: {response.text}")
    
  • 修正Endpoint格式:当前Endpoint末尾带了?book=,但参数里又传了book,会导致URL出现重复参数(如.../?book=&book=aave_usd),API可能解析错误。把Endpoint改成https://api.bitso.com/v3/ohlc/即可。
  • 处理API限流:Bitso API有请求频率限制,批量请求容易触发限流。每次请求后添加time.sleep(1),或者根据响应头的X-Rate-Limit调整间隔。
  • 增加重试机制:网络波动会导致请求失败,用重试逻辑覆盖临时故障:
    from requests.adapters import HTTPAdapter
    from urllib3.util.retry import Retry
    
    session = requests.Session()
    retry = Retry(total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504])
    adapter = HTTPAdapter(max_retries=retry)
    session.mount('https://', adapter)
    
    后续用session.get()替代requests.get()。
  • 过滤无效交易对:部分交易对可能已下线或无OHLC数据(比如usd_usd),先调用Bitso的/v3/available_books/接口获取有效交易对列表,提前过滤无效项。

内容的提问来源于stack exchange,提问作者Fffffff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 10:15:17