1. 程式人生 > 其它 >AttributeError: module 'websocket' has no attribute 'create_connection' websocket 爬蟲

AttributeError: module 'websocket' has no attribute 'create_connection' websocket 爬蟲

抓取某個網站的資料,程式碼如下

  

instrument_name = "BTC-25JUN21-28000-P"
    msg = {
        "jsonrpc": "2.0",
        "id": 3983,
        "method": "public/get_last_trades_by_instrument_and_time",
        "params": {
            "instrument_name": instrument_name,
            "start_timestamp": 1622822400000,
            
"end_timestamp": 1623427200000, "count": 1000 } } headers = { 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1' } url = 'wss://test.deribit.com/ws/api/v2' ws = websocket.create_connection(url=url, headers=headers, timeout=15
) ws.send(json.dumps(msg)) print(ws.recv())

報錯如下

  

C:\Python37\python3.exe D:/spider_telegram/spider_17_deribit/spider_2_get_content.py
Traceback (most recent call last):
File "D:/spider_telegram/spider_17_deribit/spider_2_get_content.py", line 35, in <module>
spider()
File "D:/spider_telegram/spider_17_deribit/spider_2_get_content.py
", line 28, in spider ws = websocket.create_connection(url=url, headers=headers, timeout=15) AttributeError: module 'websocket' has no attribute 'create_connection'

解決辦法:

  pip3 install websocket-client