Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 从API中提取数据_Json_Python 3.x_Websocket_Python Asyncio_Subscription - Fatal编程技术网

Json 从API中提取数据

Json 从API中提取数据,json,python-3.x,websocket,python-asyncio,subscription,Json,Python 3.x,Websocket,Python Asyncio,Subscription,我在这个问题上已经纠结了一段时间,如果能得到任何帮助,我将不胜感激。我试图从API中获取比特币和以太坊的价格,使用2个订阅。同时,我还向websocket发送一条授权消息,以登录到我的帐户 我遇到的问题是订阅代码没有像我预期的那样过滤掉最后的价格 如果我删除登录/授权消息,它将过滤输出罚款(尽管我需要授权用于其他用途),并且它只提供一次输出,而不是像订阅那样提供连续的价格流 我的代码是: import asyncio import json import websocket btc = N

我在这个问题上已经纠结了一段时间,如果能得到任何帮助,我将不胜感激。我试图从API中获取比特币和以太坊的价格,使用2个订阅。同时,我还向websocket发送一条授权消息,以登录到我的帐户

我遇到的问题是订阅代码没有像我预期的那样过滤掉最后的价格

如果我删除登录/授权消息,它将过滤输出罚款(尽管我需要授权用于其他用途),并且它只提供一次输出,而不是像订阅那样提供连续的价格流

我的代码是:

import asyncio
import json

import websocket


btc = None
eth = None

async def call_api2(msg1, msg2, msg4):
    async with websockets.connect('wss://testapp.deribit.com/ws/api/v2') as websocket:
        await websocket.send(msg1)
        btc_ticker = await websocket.recv()
        btc_ticker_load = json.loads(btc_ticker)
        #print(btc_ticker_load)
        btc_ticker = await websocket.recv()
        btc_ticker_load = json.loads(btc_ticker)
        global btc
        btc = btc_ticker_load["params"]["data"]["last_price"]
        print(btc)


        await websocket.send(msg4)
        eth_ticker = await websocket.recv()
        eth_ticker_load = json.loads(eth_ticker)
        #print(eth_ticker_load)
        eth_ticker = await websocket.recv()
        eth_ticker_load = json.loads(eth_ticker)
        global eth
        eth = eth_ticker_load["params"]["data"]["last_price"]
        print(eth, 'this is eth')
        print(btc-eth, 'this is the delta')

        await websocket.send(msg2)
        while websocket.open:
            authenticate = await websocket.recv()
            # do something with the notifications...
            authenticate_load = json.loads(authenticate)
            print(authenticate_load)


loop = asyncio.get_event_loop()
try:
  asyncio.ensure_future(call_api2( json.dumps(msg1), json.dumps(msg2), json.dumps(msg4) ))   
  loop.run_forever()
except KeyboardInterrupt:
  pass
finally:
  print('closing loop')
  loop.close()
我的输出不只是过滤btc和eth价格,而是整个输出如下:

{'jsonrpc': '2.0', 'method': 'subscription', 'params': {'channel': 'ticker.ETH-PERPETUAL.raw', 'data': {'timestamp': 1575898571330, 'stats': {'volume': 47214.31236, 'low': 148.5, 'high': 151.9}, 'state': 'open', 'settlement_price': 148.89, 'open_interest': 165337623, 'min_price': 147.26, 'max_price': 153.27, 'mark_price': 150.26, 'last_price': 150.25, 'instrument_name': 'ETH-PERPETUAL', 'index_price': 150.29, 'funding_8h': 0.0, 'current_funding': 0.0, 'best_bid_price': 150.25, 'best_bid_amount': 39114.0, 'best_ask_price': 150.3, 'best_ask_amount': 9398.0}}}

你试过单引号
?@OrestisZekai我试过,不幸的是这两种方式都没有区别。你能提供你的msg1的内容吗?谢谢,伙计们,但我设法找到了答案。在订阅流式传输数据之前,它会发送一条初始确认消息,表示订阅成功。此消息没有[“params”][“data”][“last_price”],因此我通过使用Try,Except函数解决了此问题。您可以使用字典上的
.get
,而不是使用Try/Except。在这种情况下,这不会失败,您可以给它一个默认值,而不是“无”。