我正试图从这个网站上抓取最新的出价并询问价格“http://btc-exchange.com/” 我可以看到这个socket.io
提供的价格wss://pusher.mistertango.com/socket.io/?EIO=3&transport=websocket&sid=XXX
正在通过此调用生成sessionID
https://pusher.mistertango.com/socket.io/?EIO=3&transport=polling&t=1517079662330-10
这是我目前正在使用的代码
import requests
from websocket import create_connection
import json
SID_url = "https://pusher.mistertango.com/socket.io/?EIO=3&transport=polling"
SID_req = requests.get(SID_url, headers={'User-Agent': 'Mozilla/5.0'}).text
SID = SID_req[SID_req.index("sid")+6:SID_req.index(",")-1]
print(SID_req)
print(SID)
ws = create_connection("wss://pusher.mistertango.com/socket.io/?EIO=3&transport=websocket&sid="+SID)
ws.send('2probe')
print(ws.recv())
ws.send('5')
print(ws.recv())
ws.send('42["subscribe",{"chan":"market-e559906eda4362f58bcaab40a4bfb5b4"}]')
while True:
result = ws.recv()
print(result)
ws.close()
这是代码
的输出 ÿ0{"sid":"mURV8OnaNqax_AmvAAF2","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
mURV8OnaNqax_AmvAAF2
3probe
40
我发送的消息基于我在chrome-dev工具中看到的消息。 Websocket msgs
我的连接在'40'之后停止接收任何消息。我做错了什么?
答案 0 :(得分:1)
有您想要的代码。经过测试并有效
#!/usr/bin/env python
from socketIO_client import SocketIO
def on_connect():
print('connect')
def on_disconnect():
print('disconnect')
def on_reconnect():
print('reconnect')
def on_aaa_response(*args):
print('on_aaa_response', args)
def on_bbb_response(*args):
print('on_bbb_response', args)
with SocketIO('https://pusher.mistertango.com') as socketIO:
socketIO.on('connect', on_connect)
socketIO.on('disconnect', on_disconnect)
socketIO.on('reconnect', on_reconnect)
socketIO.on('market-orderbook', on_aaa_response)
socketIO.emit('subscribe', {'chan': 'market-orderbook'}, on_bbb_response)
socketIO.wait_for_callbacks(seconds=10)
答案 1 :(得分:0)
您可以使用asyncio和websocket来解决您的问题。 您可以使用以下命令安装websocket
pip install websocket
之后,使用以下代码片段作为与Web服务器通信的示例:
import asyncio
import websockets
async def responseFunc(uri):
async with websockets.connect(uri) as websocket:
await websocket.send("Hello world!")
url = "wss://pusher.mistertango.com/socket.io/?EIO=3&transport=websocket&sid=XXX"
asyncio.get_event_loop().run_until_complete(
responseFunc(url))