回答

收藏

websocket 协议的弹幕抓取

问答交流 问答交流 1766 人阅读 | 0 人回复 | 2020-02-06


请问有没有大佬做过 websocket 协议的弹幕抓取?用 python2.0+ 抓取是没问题的,但是用 python3.0 就不行了,请问是怎么回事?万分感谢
import websocket

try:
    import thread
except ImportError:
    import _thread as thread
import time


def on_message(ws, message):
    print(message)


def on_error(ws, error):
    print(error)


def on_close(ws):
    print("### closed ###")


def str2hex(str_):
    hex_ = [ord(i) for i in str_]
    return hex_


def int2str(int_):
    str_ = ''    for i in int_:
        str_ += chr(i)
    return str_


def hex2dec(string_num):
    return str(int(string_num.upper(), 16))


def on_open(ws):
    part1 = [8, 200, 1, 26, 135, 1, 10, 88, 57, 103, 52, 112, 101, 54, 81, 86, 122, 106, 111, 80, 47, 120, 88, 65, 74,             68, 71, 88, 80, 116, 73, 68, 71, 69, 70, 115, 90, 52, 78, 67, 43, 73, 49, 105, 48, 113, 47, 66, 97, 67, 82,             87, 48, 47, 120, 119, 43, 106, 43, 70, 97, 88, 66, 118, 69, 85, 66, 118, 116, 52, 79, 87, 122, 114, 117,             104, 99, 65, 83, 106, 110, 51, 108, 104, 71, 51, 103, 65, 101, 102, 57, 70, 43, 81, 61, 61, 18, 11]
    part3 = [58, 30, 105, 113, 85, 87, 70, 115, 126, 97, 49, 65, 52, 65, 108, 119, 49, 81, 95, 49, 53, 51, 54, 53, 56,             54, 51, 54, 55, 52, 54, 57]

    part2 = str2hex('DrfGTNZDyyM')

    d = part1 + part2 + part3
    ws.send(d, websocket.ABNF.OPCODE_BINARY)

    # heart beat thread start    def run():    while (True):
        time.sleep(2)
        x = [8, 1, 26, 7, 8, 184, 232, 190, 199, 220, 44]
        ws.send(x, websocket.ABNF.OPCODE_BINARY)


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://live-ws-pg-group2.xxx.com/websocket", on_message=on_message, on_error=on_error,                                on_close=on_close)
    ws.on_open = on_open
    ws.run_forever(skip_utf8_validation=True)
报错:

error from callback <function on_open at 0x10a487320>: a bytes-like object is required, not 'list'

我知道是说
ws.send(d, websocket.ABNF.OPCODE_BINARY)
这里不应该传 list,而是byte,但是在2.0就是这样传的;主要我也不知道在3.0该怎么改。
分享到:
回复

使用道具 举报