如何从websocket保存数据?

2024-06-02 10:31:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经成功订阅了一个websocket,正在接收数据。我正在等待保存我的数据,以便在数据框中使用它进行进一步分析。在

到目前为止,我的代码只返回空列表和数据帧。在

代码: 返回空列表

wsClient = GDAX.WebsocketClient(url="wss://ws-feed.gdax.com", products="LTC-USD")

df1 = []

for i in wsClient.start():
    df1 = df1.append(wsClient.start())

代码: 返回空列表和数据帧

^{pr2}$

Tags: 数据代码url列表wsfeedgdaxstart
1条回答
网友
1楼 · 发布于 2024-06-02 10:31:50

您需要实现您自己的自定义on_message方法才能获得websocket信息:

import time
import gdax
import pandas as pd

results = []

class myWebsocketClient(gdax.WebsocketClient):
    def on_open(self):
        self.url = "wss://ws-feed.gdax.com/"
        self.products = ["LTC-USD"]

    def on_message(self, msg):
        if 'price' in msg and 'type' in msg:
            results.append(msg['price'])

wsClient = myWebsocketClient()
wsClient.start()

time.sleep(5)

df = pd.DataFrame(results, columns = ["Price"])
print(df.head())
wsClient.close()

这将运行5秒,并输出:

^{pr2}$

相关问题 更多 >