python - 订阅 websocket 后如何解析数据
问题描述
这是我用来订阅 websocket 的代码。
for i in range(3):
try:
ws = create_connection('wss://www.bitmex.com/realtime?subscribe=trade:XBTUSD')
except Exception as error:
print('Error')
time.sleep(3)
else:
break
while True:
try:
result = ws.recv()
result = json.loads(result)
print(f"{result}")
except Exception as error:
print("Error")
time.sleep(3)
ws.close()
这是我得到回报的一个例子
{'success': True, 'subscribe': 'trade:XBTUSD', 'request': {'op': 'subscribe', 'args': 'trade:XBTUSD'}}
{'table': 'trade', 'action': 'partial', 'keys': [], 'types': {'timestamp': 'timestamp', 'symbol': 'symbol', 'side': 'symbol', 'size': 'long', 'price': 'float', 'tickDirection': 'symbol', 'trdMatchID': 'guid', 'grossValue': 'long', 'homeNotional': 'float', 'foreignNotional': 'float'}, 'foreignKeys': {'symbol': 'instrument', 'side': 'side'}, 'attributes': {'timestamp': 'sorted', 'symbol': 'grouped'}, 'filter': {'symbol': 'XBTUSD'}, 'data': [{'timestamp': '2019-11-27T03:04:30.081Z', 'symbol': 'XBTUSD', 'side': 'Buy', 'size': 1000, 'price': 7149, 'tickDirection': 'ZeroPlusTick', 'trdMatchID': 'e235b829-dde5-1eda-dc51-d66de2fbd0e7', 'grossValue': 13988000,
'homeNotional': 0.13988, 'foreignNotional': 1000}]}
如果我使用的是其余 api 并且这是一次性请求,我可以毫无问题地提取例如“side”和“size”值,但是当我不断获得新的订阅时,我对如何通过订阅来做到这一点有点困惑来自 websocket 的数据。谢谢你的帮助。
解决方案
推荐阅读
- vba - 如何修改此 VBA 代码以使其在我的工作簿中的所有工作表上运行?
- python - 您如何在 while 循环中延迟特定事件?
- unity3d - 团结 | webCamScript 不显示
- python - 获取列表的剩余部分
- python - 使用virtualenv时将python2.7包改成我需要的python3.5包
- google-bigquery - 将数据从 csv 文件加载到大查询中时 Json 模式文件的目的
- java - 带窗口的 KTable 产生错误的类型
- openmpi - OpenMPI 进程亲和性
- android - 以灰色显示的菜单项图标颜色不显示其原始颜色
- python - 如何使用 OpenCV 将图像的一部分复制到另一个图像?