Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

android okhttp3 websokcet 直播间爬虫报错

'''
@author:stephen
@time 2020/12/24
'''
import websocket
import time
try:
    import thread
except ImportError:
    import _thread as thread
class websocket_class:
    def __init__(self):
        pass
 def on_message(ws, message):
        print(message)
    def on_error(ws, error):
        print(error)
    # 关闭websocket长连接
 def on_close(ws):
        print("关闭连接")
    def on_open(ws):
        def run(*args):
            for i in range(3):
                time.sleep(1)
                ws.send("Hello %d" % i)
            time.sleep(1)
            ws.close()
            print("thread terminating...")
        thread.start_new_thread(run, ())
if __name__ == "__main__":
    cookie = "d_ticket=8c7959af9256f904126faa3cdc8b2df222907; odin_tt=d7d47761b0398c1c643cec118a7340a0622beeac768bf6b5fc6f5f3023bdca59100973a35d09a010c559b256c6f37994a33baae44ef43989cdfebead46643031; passport_csrf_token=cec8371850d90e9f2f0a2c278839b221; sessionid=dcecb2f22da348382779899d24c84534; sessionid_ss=dcecb2f22da348382779899d24c84534; "
 remoute_url = "wss://webcast3-ws-c-lq.amemv.com/webcast/im/push/?aid=1128&device_id=68340616007&access_key=e5a5d78a5d3d4b471e280ab4b96edb68&fpid=9&sdk_version=3&iid=2919951127820444&sid=dcecb2f22da348382779899d24c84534&pl=1&ne=1&version_code=140000&is_background=-1&ac=WIFI&aid=1128&appTheme=dark&app_language=zh&app_name=aweme&app_version=14.0.0&build_number=140013&cdid=70E802B3-46D4-4F8E-BD94-4ECE805C5168&channel=App%20Store&compress=gzip&current_network_quality_info={%22video_download_speed%22:977,%22downstream_throughput_kbps%22:8050,%22quic_rtt%22:89,%22quic_receive_loss_rate%22:-1,%22http_rtt%22:89,%22net_effective_connection_type%22:5,%22quic_send_loss_rate%22:-1,%22tcp_rtt%22:89}&cursor=1608802468536_6909753985784164887_1_1&device_id=68340616007&device_platform=iphone&device_type=iPhone%20X&effect_sdk_version=8.2.0&identity=audience&idfa=8A1736A8-BC2A-4914-8064-C36670461750&iid=2919951127820444&imprp=d4CkKCuMkRRu8&internal_ext=fetch_time:1608802468536|start_time:0|flag:0|seq:1|next_cursor:1608802468536_6909753985784164887_1_1|door:2-n2&is_vcd=1&js_sdk_version=1.91.0.3&language=en-TW&last_rtt=966&live_id=1&live_ws_local_identify=live_ws_identify_local&mcc_mnc=46002&openudid=db5050c2560532cf159e246cfae47c838f8aae4c&os_api=18&os_version=13.7&parse_cnt=4&pass-region=0&pass-route=0&recv_cnt=4&resp_content_type=protobuf&room_id=6909706321642277646&screen_width=375&sid=dcecb2f22da348382779899d24c84534&tma_jssdk_version=1.91.0.3&version_code=14.0.0&vid=2FBA1918-DBCF-4AFB-86FB-18A0F229145F&webcast_gps_access=1&webcast_language=zh&webcast_locale=zh-Hans_CN&webcast_sdk_version=1810"
 headers = {
        'Host': 'webcast3-ws-c-lf.amemv.com',
 'Connection': 'Upgrade',
 'Cache-Control': 'no-cache',
 'Upgrade': 'websocket',
 'Origin': 'wss://webcast3-ws-c-lf.amemv.com',
 'Sec-WebSocket-Version': '13',
 'x-tt-trace-id': '00-93dbaf7309fe96b0b47bc06b7a590468-93dbaf7309fe96b0-01',
 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Cronet Mobile/15E148 Safari/605.1',
 'Accept-Encoding': 'gzip, deflate, br',
 'Cookie': cookie,
 'Sec-WebSocket-Key': 'zLMMMqeMYJjTJGl6w4yDiQ==',
 'Accept-Encoding': 'gzip, deflate, br',
 'Accept-Language': 'zh-CN,zh;q=0.9,',
 'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
 'Sec-WebSocket-Protocol': 'pbbp2',
 }
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(remoute_url,
 on_message=websocket_class.on_message,
 on_error=websocket_class.on_error,
 on_close=websocket_class.on_close,
 header=headers)
    ws.on_open = websocket_class.on_open
    ws.run_forever()

报错如下
好像是握手失败,麻烦做过websocket 爬虫的指教一下

自我感觉是headers 参数有问题

D:loadingpython3.7python.exe C:/Users/secoo/PycharmProjects/test/main.py
--- request header ---
GET /webcast/im/push/?aid=1128&device_id=68340616007&access_key=e5a5d78a5d3d4b471e280ab4b96edb68&fpid=9&sdk_version=3&iid=2919951127820444&sid=dcecb2f22da348382779899d24c84534&pl=1&ne=1&version_code=140000&is_background=-1&ac=WIFI&aid=1128&appTheme=dark&app_language=zh&app_name=aweme&app_version=14.0.0&build_number=140013&cdid=70E802B3-46D4-4F8E-BD94-4ECE805C5168&channel=App%20Store&compress=gzip&current_network_quality_info={%22video_download_speed%22:977,%22downstream_throughput_kbps%22:8050,%22quic_rtt%22:89,%22quic_receive_loss_rate%22:-1,%22http_rtt%22:89,%22net_effective_connection_type%22:5,%22quic_send_loss_rate%22:-1,%22tcp_rtt%22:89}&cursor=1608802468536_6909753985784164887_1_1&device_id=68340616007&device_platform=iphone&device_type=iPhone%20X&effect_sdk_version=8.2.0&identity=audience&idfa=8A1736A8-BC2A-4914-8064-C36670461750&iid=2919951127820444&imprp=d4CkKCuMkRRu8&internal_ext=fetch_time:1608802468536|start_time:0|flag:0|seq:1|next_cursor:1608802468536_6909753985784164887_1_1|door:2-n2&is_vcd=1&js_sdk_version=1.91.0.3&language=en-TW&last_rtt=966&live_id=1&live_ws_local_identify=live_ws_identify_local&mcc_mnc=46002&openudid=db5050c2560532cf159e246cfae47c838f8aae4c&os_api=18&os_version=13.7&parse_cnt=4&pass-region=0&pass-route=0&recv_cnt=4&resp_content_type=protobuf&room_id=6909706321642277646&screen_width=375&sid=dcecb2f22da348382779899d24c84534&tma_jssdk_version=1.91.0.3&version_code=14.0.0&vid=2FBA1918-DBCF-4AFB-86FB-18A0F229145F&webcast_gps_access=1&webcast_language=zh&webcast_locale=zh-Hans_CN&webcast_sdk_version=1810 HTTP/1.1
Upgrade: websocket
Host: webcast3-ws-c-lq.amemv.com
Origin: http://webcast3-ws-c-lq.amemv.com
Connection: upgrade
Host: webcast3-ws-c-lf.amemv.com
Connection: Upgrade
Cache-Control: no-cache
Upgrade: websocket
Origin: wss://webcast3-ws-c-lf.amemv.com
Sec-WebSocket-Version: 13
x-tt-trace-id: 00-93dbaf7309fe96b0b47bc06b7a590468-93dbaf7309fe96b0-01
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Cronet Mobile/15E148 Safari/605.1
Accept-Encoding: gzip, deflate, br
Cookie: d_ticket=8c7959af9256f904126faa3cdc8b2df222907; odin_tt=d7d47761b0398c1c643cec118a7340a0622beeac768bf6b5fc6f5f3023bdca59100973a35d09a010c559b256c6f37994a33baae44ef43989cdfebead46643031; passport_csrf_token=cec8371850d90e9f2f0a2c278839b221; sessionid=dcecb2f22da348382779899d24c84534; sessionid_ss=dcecb2f22da348382779899d24c84534; 
Sec-WebSocket-Key: zLMMMqeMYJjTJGl6w4yDiQ==
Accept-Language: zh-CN,zh;q=0.9,
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Protocol: pbbp2


-----------------------
--- response header ---
HTTP/1.1 200 OK
Server: Tengine
Content-Type: application/octet-stream
Content-Length: 0
Connection: keep-alive
Date: Thu, 24 Dec 2020 10:29:12 GMT
Handshake-Msg: auth failed
Handshake-Status: 417
tt-idc-switch: 10000@20201223001
Access-Control-Expose-Headers: tt-idc-switch
server-timing: inner; dur=4
Via: cache16.cn2487[28,0]
Timing-Allow-Origin: *
EagleId: 6548fe4216088057521198868e
-----------------------
Handshake status 200 OK
关闭连接

Process finished with exit code 0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...