我正在尝试从网站中提取一些数据。我遇到的问题是它会提取数据值,然后继续不断地重新打印它,而不是提取最新的实时数据值并更新它。我从https://github.com/BitMEX/api-connectors/tree/master/official-ws/python获得了代码并进行了一些更改。
from bitmex_websocket import BitMEXWebsocket
import logging
from time import sleep
# Basic use of websocket.
def run():
logger = setup_logger()
# Instantiating the WS will make it connect. Be sure to add your api_key/api_secret.
ws = BitMEXWebsocket(endpoint="https://www.bitmex.com/api/v1", symbol="XBTUSD",
api_key=None, api_secret=None)
logger.info("Instrument data: %s" % ws.get_instrument())
# Run forever
while(ws.ws.sock.connected):
logger.info("Ticker: %s" % ws.get_ticker())
if ws.api_key:
logger.info("Funds: %s" % ws.funds())
#logger.info("Market Depth: %s" % ws.market_depth())
(logger.info("Recent Trades: %s\n\n" % ws.recent_trades()[0]["size"]))
sleep(1)
def setup_logger():
# Prints logger info to terminal
logger = logging.getLogger()
logger.setLevel(logging.INFO) # Change this to DEBUG if you want a lot more info
ch = logging.StreamHandler()
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# add formatter to ch
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
if __name__ == "__main__":
run()