tushare报错之解决(亲测有效):websocket._exceptions,ValueError: No ‘:‘ found when decoding object value

tushare报错:websocket._exceptions,ValueError: No ':' found when decoding object value

一、修改后结果

在这里插入图片描述

二、报错

第一个报错:

websocket._exceptions

具体含义是websocket中缺失_exceptions,

github上:
在这里插入图片描述

pip uninstall websocker
pip install websocket-client

无效

准确解决方法:换成1.2.50版本

第二个报错:

ValueError: No ':' found when decoding object value

格式解码不对

解决方法:

来源:链接: tushare 之get_today_all修复接口完整code.

text = text.replace('""', '"')

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

如上所述:无效

解决方法二:
出错的原因在于:新浪接口返回的数据已经修改了,原来的提取数据的方法就出现错误

准确解决方法:

将stock/trading.py源文件的_parsing_dayprice_json()函数为:中的这些代码注释掉

#reg = re.compile(r',(.*?):')
#text = reg.sub(r',"\1":', text.decode('gbk') if ct.PY3 else text)
t#ext = text.replace('"{symbol', '{"symbol')
#text = text.replace('{symbol', '{"symbol"')
#if ct.PY3:
#jstr = json.dumps(text)
#else:
#jstr = json.dumps(text, encoding='GBK')
#js = json.loads(jstr)

换成

js = text.decode('utf8')  

在这里插入图片描述

三、成功获取

在这里插入图片描述
在这里插入图片描述

这个错误通常是由于在 WebSocket 连接已关闭的情况下,尝试发送或接收消息。为了防止这个错误,你可以在发送消息之前检查 WebSocket 连接的状态,如果连接已关闭,则不发送消息。下面是一个修改后的示例: ```python import websocket import threading class WebSocketClient: def __init__(self, url): self.url = url self.ws = None self.connect() def connect(self): self.ws = websocket.WebSocketApp(self.url, on_open=self.on_open, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close) self.wst = threading.Thread(target=self.ws.run_forever) self.wst.daemon = True self.wst.start() def on_open(self, ws): print("WebSocket connection established") def on_message(self, ws, message): print("Received message:", message) def on_error(self, ws, error): print("WebSocket error:", error) def on_close(self, ws): print("WebSocket connection closed") def send(self, message): if self.ws and self.ws.sock and self.ws.sock.connected: self.ws.send(message) else: print("WebSocket connection is closed.") def close(self): self.ws.close() if __name__ == "__main__": ws = WebSocketClient("ws://localhost:8080") ws.send("Hello, WebSocket Server!") ``` 在 `send` 方法中,我们添加了一些代码来检查 WebSocket 连接的状态。如果连接已关闭,则不发送消息,并打印一条错误信息。这样,在尝试发送消息之前,我们始终会检查 WebSocket 连接的状态,从而避免出现 `WebSocketConnectionClosedException` 异常。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值