我正在开发一种电报机器人,该机器人从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."}'
答案 0 :(得分:2)
您缺少必填参数符号和间隔,查询应如下所示:
https://api.binance.com/api/v1/klines?symbol=BTCUSDT&interval=1h
您只需导入请求:
import requests
market = 'BTCUSDT'
tick_interval = '1h'
url = 'https://api.binance.com/api/v1/klines?symbol='+market+'&interval='+tick_interval
data = requests.get(url).json()
print data
请在此处查看官方的Binance API文档:https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md
答案 1 :(得分:0)
请求 python 包有一个 params
, json
参数,因此您不需要导入任何您正在导入的包。
import requests
url = 'https://api.binance.com/api/v3/klines'
params = {
'symbol': 'BTCUSDT',
'interval': '1h'
}
response = requests.get(url, params=params)
print(response.json())