简体   繁体   English

在 Python 中从 Binance 获取数据

[英]Get data from Binance in Python

I am trying to use BinanceSocketManager fromhttps://python-binance.readthedocs.io/en/latest/websockets.html and do not understand what I'm doing wrong?我正在尝试使用 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())

and get this error并得到这个错误

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>

I understand that I have two issues, with Python 3.10 and with the closing session.我知道我有两个问题,Python 3.10 和关闭 session。 But even VSCode highlight但即使是 VSCode 也高亮

  await client.close_connection()

as "This code is unreachable".作为“此代码无法访问”。 Please help to fix this code.请帮助修复此代码。 Thnx.谢谢。

You can use websocket-client package on python and this snippet of code您可以在 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()

The was URL used in this example is for getting prices, you can change it to whatever URL available in WS binance docs此示例中使用的 URL 用于获取价格,您可以将其更改为 WS binance 文档中可用的任何 URL

You are getting the error because of an incompatibility with python 3.10由于与 python 3.10 不兼容,您收到错误消息

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

Downgrade to python3.8 and try again降级到python3.8再试一次

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

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