我试图通过自己连接到webscoket来刮取具有websocket连接的网页。这是我的代码:
from websocket import create_connection
import json
headers = json.dumps({
'Connection': 'Upgrade',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Upgrade': 'websocket',
'Origin': 'https://www.bet777.be',
'Sec-WebSocket-Version': 13,
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'es,en;q=0.9,pl;q=0.8,es-AR;q=0.7',
'Sec-WebSocket-Key': 'mYc+hqhy8sUyeilrxyxSPA==',
'Sec-WebSocket-Extensions': 'permessage-deflate;
client_max_window_bits',
})
ws = create_connection('wss://pushserver-
uk.sbtech.com/signalr/connecttransport=webSockets&clientProtocol=1.5&connectionToken=nOugyCC54kCePwbLVXCfkfpxZsipI83mU476SdYNspEAD2U0%2F3O44lja67ujJErljZiflHtWyOwELt0OHfQhBQxXu14hVe8zE%2Fr4syolXWBCWWoG%2B2D8WwmUCxi5HSUz4&connectionData=%5B%7B%22name%22%3A%22communicationhub%22%7D%5D&tid=9', header=headers)
ws.send('''{"H":"communicationhub","I":0,"M":"Introduce"}''')
while True:
print(ws.recv)
我收到以下错误:
File "/home/gonzalo/.local/lib/python3.6/site-packages/websocket/_handshake.py", line 124, in _get_handshake_headers
key = options['header']['Sec-WebSocket-Key']
TypeError: string indices must be integers
如果我删除了Sec-WebSocket-Key部分,我会收到400状态响应。
我知道我做错了什么?
编辑:使用dict而不是json.dump得到以下错误。文件“/home/gonzalo/.local/lib/python3.6/sitepackages/websocket/_handshake.py”,第139行,如果v不是None
TypeError:序列项1:预期的str实例,找到int
header
的websocket.create_connection
参数应该是dict,而不是JSON字符串,所以不使用json.dumps
将dict转换为字符串,而是直接将dict作为标题传递:
headers = {
'Connection': 'Upgrade',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Upgrade': 'websocket',
'Origin': 'https://www.bet777.be',
'Sec-WebSocket-Version': '13',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'es,en;q=0.9,pl;q=0.8,es-AR;q=0.7',
'Sec-WebSocket-Key': 'mYc+hqhy8sUyeilrxyxSPA==',
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
}
ws = create_connection('wss://pushserver-uk.sbtech.com/signalr/connecttransport=webSockets&clientProtocol=1.5&connectionToken=nOugyCC54kCePwbLVXCfkfpxZsipI83mU476SdYNspEAD2U0%2F3O44lja67ujJErljZiflHtWyOwELt0OHfQhBQxXu14hVe8zE%2Fr4syolXWBCWWoG%2B2D8WwmUCxi5HSUz4&connectionData=%5B%7B%22name%22%3A%22communicationhub%22%7D%5D&tid=9',
header=headers)