业余程序员在这里从事一个周末项目。
我希望访问此处提供的可公开使用的API:https : //api.coinsecure.in/它为我们提供了BitCoin交易数据-该API是通过我不熟悉的websockets实现的。
Websocket URI是wss://coinsecure.in/websocket,我希望测试的方法是:{“ method”:“ recentbuytrades”}
我可以使用Python中的“ websocket-client”访问WebScocket API,如下所示:https ://pypi.python.org/pypi/websocket-client/
但不幸的是,我无法弄清楚如何为特定方法检索数据-{“方法”:“ recentbuytrades”}
非常感谢您提供的有关为此特定方法提取数据的任何指导。
最好,瑞安
[编辑]我正在使用的当前代码是这样的:
from websocket import create_connection
ws = create_connection("wss://coinsecure.in/websocket")
result = ws.recv()
print ("Received '%s'" % result)
ws.close()
试试这个:
from websocket import create_connection
ws = create_connection("wss://coinsecure.in/websocket")
ws.send('{"method": "recentbuytrades"}')
while True:
result = ws.recv()
print ("Received '%s'" % result)
ws.close()
注意该ws.send()
方法,该方法告诉API您想要什么。接下来,while True
无限循环-WebSockets是无限连接;信息通常不止一次通过它们发送。您将从服务器(看起来像JSON)那里获得一堆信息(一个“框架”),进行处理,然后等待下一组信息。
看起来API还会向您发送不必要的数据。如果框架不包含recentbuytrades
钥匙,则可能要扔掉。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句