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

如何将Binance API返回的行情数据从Google Colab导出至Google Sheets

解决方案

步骤1:从Binance API响应中提取指定字段

假设你调用Binance API后得到的响应数据是列表格式(比如调用bookTicker接口返回多交易对数据),先把需要的symbol、bidPrice、askPrice字段提取出来,整理成适合Google Sheets导入的二维数组(第一行是表头,后续为数据行)。

示例代码:

# 假设已通过Binance API获取行情数据,存储在binance_data变量中
# binance_data 格式示例:[{'symbol': 'BTCUSDT', 'bidPrice': '45000.00', ...}, ...]

# 定义需要提取的目标字段
target_fields = ['symbol', 'bidPrice', 'askPrice']

# 整理导出数据:先加表头,再逐行提取字段值
export_data = [target_fields]
for item in binance_data:
    # 按字段顺序提取,可按需转换数据类型(比如价格转float)
    row = [
        item['symbol'],
        float(item['bidPrice']),
        float(item['askPrice'])
    ]
    export_data.append(row)

如果是单个交易对的API响应(比如指定symbol调用/api/v3/ticker/price),同样整理成二维数组:

# 假设单个交易对数据:single_pair_data = {'symbol': 'ETHUSDT', 'bidPrice': '2500.00', ...}
export_data = [target_fields]
export_data.append([
    single_pair_data['symbol'],
    float(single_pair_data['bidPrice']),
    float(single_pair_data['askPrice'])
])

步骤2:将整理好的数据传入Google Sheets导出代码

假设你已完成Colab与Google Sheets的授权(通过gspread和google-auth),直接把export_data传入写入逻辑即可。

示例导出代码(基于gspread):

import gspread
from google.auth import default

# 授权流程(已完成授权可跳过)
creds, _ = default()
client = gspread.authorize(creds)

# 打开目标Google Sheet(替换为你的表名)
sheet = client.open("Binance行情数据").sheet1

# 可选:清空原有数据
sheet.clear()

# 从A1单元格开始写入整理好的数据
sheet.update('A1', export_data, value_input_option='USER_ENTERED')

关键注意事项

  • 必须保证export_data是二维列表格式,gspread的update方法可直接按行列识别写入。
  • value_input_option='USER_ENTERED'会让Google Sheets自动识别数据类型(比如把数字转为数值格式而非文本)。
  • API返回的价格通常是字符串类型,建议转换为float/int,避免Sheet中显示为文本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 20:10:28