查询等
Python
#!/usr/bin/env python3
# 查询币种的信息,但不包括价格
# coding: utf-8
import requests
import json
from urllib.parse import urlencode
class HostInfo:
def __init__(self, host, prefix, headers):
self.host = host
self.prefix = prefix
self.headers = headers
def __str__(self):
return("host = {}, \nprefix = {}, \nheaders = {}").format(self.host, self.prefix, self.headers)
def search_info(host_info, url, query_param = {}):
if (query_param == {}):
query_url = host_info.host + host_info.prefix + url
else:
# 将字典转换为查询字符串
query_string = urlencode(query_param)
query_url = host_info.host + host_info.prefix + url + "?" + query_string
ret = requests.request('GET', query_url, headers = host_info.headers)
print(query_url)
return ret
def main():
host = "https://api.gateio.ws"
prefix = "/api/v4"
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
host_info = HostInfo(host, prefix, headers)
print(host_info)
# url = '/spot/currencies' # 查询全部币种
# url = '/spot/currencies/GT' # 查询单个币种
# url = '/spot/currency_pairs/BTC_USDT' # 查询单个交易信息
# url = '/spot/tickers' # 查询单个交易信息
# query_param = "currency_pair=BTC_USDT" # 可省参数
url = '/spot/order_book' # 查询单个交易信息
query_param = {
'currency_pair' : "BTC_USDT",
'interval' : "10"
}
ret = search_info(host_info, url, query_param)
print(ret.json()) # 输出格式没有下面的好
# 使用 json.dumps() 并设置 indent 参数来格式化输出
formatted_json = json.dumps(ret.json(), indent=4)
print(formatted_json)
if __name__ == "__main__":
main()