1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import requests import pandas as pd from datetime import datetime
def get_single_tricker_data(symbol):
ticker_url = f'https://www.okx.com/api/v5/market/ticker?instId={symbol}-SWAP' response_object = requests.get(ticker_url)
json_object = response_object.json() raw_df = pd.DataFrame(json_object)
raw_df = pd.json_normalize(json_object,record_path=['data']) # 将内嵌的数据完整的解析出来
ticker_df = pd.DataFrame(index=[0],columns=['datetime','symbol','last']) ticker_df['datetime'] = pd.to_datetime(datetime.utcnow()) ticker_df['symbol'] = symbol.replace('-','/').lower() ticker_df['last'] = raw_df['last']
return ticker_df
def get_tickers_data(symbols):
tickers_df = pd.DataFrame() for symbol in symbols: ticker_df = get_single_tricker_data(symbol) tickers_df = tickers_df.append(ticker_df)
return tickers_df
def main():
# symbol = 'BTC-USDT-SWAP' # tricker_df = get_single_tricker_data(symbol) # print(tricker_df)
symbols = ['BTC-USDT','ETH-USDT','YGG-USDT','1INCH-USDT','DYDX-USDT','CRV-USDT', 'UNI-USDT','ENS-USDT' ] tickers_df = get_tickers_data(symbols) print(tickers_df)
if __name__ == '__main__': main()
|