【问题标题】:Fetch Candlestick/Kline data from Binance API using Python (preferably requests) to get JSON Dat使用 Python(最好是请求)从 Binance API 获取烛台/Kline 数据以获取 JSON 数据
【发布时间】:2018-12-23 18:49:18
【问题描述】:

我正在开发一个可以从 Binance API 获取烛台数据的电报机器人。我无法获得 JSON 数据作为响应。以下代码是我尝试过的。

 import requests 

 import json

 import urllib.request

`url = "https://api.binance.com/api/v1/klines"

response = requests.request("GET", url)
print(response.text)`

期望的输出:

[ [ 1499040000000, // Open time "0.01634790", // Open "0.80000000", // High "0.01575800", // Low "0.01577100", // Close "148976.11427815", // Volume 1499644799999, // Close time "2434.19055334", // Quote asset volume 308, // Number of trades "1756.87402397", // Taker buy base asset volume "28.46694368", // Taker buy quote asset volume "17928899.62484339" // Ignore ] ]

问题已编辑:

我得到的输出是:

 `{"code":-1102,"msg":"Mandatory parameter 'symbol' was not sent, was empty/null, or malformed."}'

【问题讨论】:

  • 你得到什么错误/信息?
  • 我得到的输出是:<Response [400]>

标签: python json api candlestick-chart binance


【解决方案1】:

你缺少强制参数符号和间隔,查询应该是这样的:

https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1h

您只需要导入请求:

import requests

market = 'BTCUSDT'
tick_interval = '1h'

url = 'https://api.binance.com/api/v3/klines?symbol='+market+'&interval='+tick_interval
data = requests.get(url).json()

print(data)

请在此处查看官方币安 REST API 文档:https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md

【讨论】:

【解决方案2】:

requests python 包有一个paramsjson 参数,所以你不需要导入任何你正在导入的包。

import requests

url = 'https://api.binance.com/api/v3/klines'
params = {
  'symbol': 'BTCUSDT',
  'interval': '1h'
}
response = requests.get(url, params=params)
print(response.json())

【讨论】:

  • 最后一行应该是:print(response.json()) 而不是:print(response.json)
猜你喜欢
  • 1970-01-01
  • 2022-07-23
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 2020-05-17
  • 2020-08-15
  • 1970-01-01
相关资源
最近更新 更多