繁体   English   中英

在 Python 中从 Binance 获取数据

[英]Get data from Binance in Python

我正在尝试使用 https://python-binance.readthedocs.io/en/latest/websockets.html 中的 BinanceSocketManager 并且不明白我做错了什么?

import asyncio
from binance import AsyncClient, BinanceSocketManager
from setting import API_KEY, SECRET_KEY



async def main():
    client = await AsyncClient.create(API_KEY, SECRET_KEY)
    bm = BinanceSocketManager(client)
    # start any sockets here, i.e a trade socket
    ts = bm.trade_socket('BNBBTC')
    # then start receiving messages
    async with ts as tscm:
        while True:
            res = await tscm.recv()
            print(res)

    await client.close_connection()

if __name__ == "__main__":

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

并得到这个错误

TypeError: As of 3.10, the *loop* parameter was removed from Queue() since it is no longer necessary
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000020624729F60>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x000002062482B340>, 287815.875)]']
connector: <aiohttp.connector.TCPConnector object at 0x000002062472A0B0>

我知道我有两个问题,Python 3.10 和关闭 session。 但即使是 VSCode 也高亮

  await client.close_connection()

作为“此代码无法访问”。 请帮助修复此代码。 谢谢。

您可以在 python 上使用 websocket-client package 和这段代码

def on_open(ws):
    print('WS binance opened !!')

def on_message3_binance(ws, message):
    tokens = json.loads(message)
    

def on_error_binance(ws, error):
    print(error)

def on_close_binance(ws, close_status_code, close_msg):
    print("### closed  ###")
    


socket = f'wss://stream.binance.com:9443/ws/!ticker@arr'
ws = websocket.WebSocketApp(socket,on_open=on_open, on_message=on_message3_binance, on_error=on_error_binance,
                            on_close=on_close_binance)
ws.run_forever()

此示例中使用的 URL 用于获取价格,您可以将其更改为 WS binance 文档中可用的任何 URL

由于与 python 3.10 不兼容,您收到错误消息

TypeError: As of 3.10, the *loop* parameter was removed from Queue() since it is no longer necessary

降级到python3.8再试一次

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM