簡體   English   中英

RestAPI 過濾器參數 JSON

[英]RestAPI filter params JSON

我正在嘗試從 bitmex API 獲取最后的數據

基礎 URI: https://www.bitmex.com/api/v1

我真的不明白如何使用過濾器獲取最后的數據(從今天開始): https://www.bitmex.com/app/restAPI

這是我的代碼:

from datetime import date

import requests
import json
import pandas as pd



today = date.today()
d1 = today.strftime("%Y-%m-%d")
#print("d1 =", d1)

def parser():

 today = date.today()

 # yy/dd/mm
 d1 = today.strftime("%Y-%m-%d")
 # print("d1 =", d1)
 return f'https://www.bitmex.com/api/v1/trade?symbol=.BVOL24H&startTime={d1}&timestamp.time=12:00:00.000&columns=price'


# Making a get request
response = requests.get(parser()).json()
# print(response)

for elem in response:
  print(elem)

回應是:

 ...
{'symbol': '.BVOL24H', 'timestamp': '2021-12-27T08:05:00.000Z', 'price': 2.02}
{'symbol': '.BVOL24H', 'timestamp': '2021-12-27T08:10:00.000Z', 'price': 2.02}
{'symbol': '.BVOL24H', 'timestamp': '2021-12-27T08:15:00.000Z', 'price': 2.02}

它錯過了幾個小時,我嘗試使用 endTime、StartTime 和 Count 但沒有成功。我想我需要傳遞另一個過濾器,比如 endtime = now 和 timestamp.time = now 但我不知道如何發送有效負載或如何發送對它進行 url 編碼。

您可以使用下面的 & 向 url 添加其他參數。

'https://www.bitmex.com/api/v1/trade?symbol=.BVOL24H&startTime={d1}&timestamp.time=12:00:00.000&columns=price&endTime={date.today()}&timestamp.time={日期.今天()}'

正如過濾部分所說

許多表端點采用過濾器參數。 這預計是 JSON

這些參數不是查詢字符串中的鍵,而是filter鍵中給定的字典中的鍵

url = "https://www.bitmex.com/api/v1/trade"
filters = {
    'startTime': date(2021, 12, 20).strftime("%Y-%m-%d"),
    'timestamp.time': '12:00:00.000'
}
params = {
    'symbol': '.BVOL24H',
    'filter': json.dumps(filters),
}
response = requests.get(url, params=params)
for elem in response.json():
    print(elem)

例子

/trade?symbol=.BVOL24H&filter={%22startTime%22:%222021-12-20%22,%22timestamp.time%22:%2212:00:00.000%22}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM