当前位置 博文首页 > 小白的成长记录:WSS协议网页爬取(状态码101)

    小白的成长记录:WSS协议网页爬取(状态码101)

    作者:[db:作者] 时间:2021-07-06 12:28

    这里用AioWebSocket库做演示

    AioWebSocket是一个遵循 WebSocket 规范的 异步 WebSocket 客户端,相对于其他库它更轻、更快。

    Installation

    pip install aiowebsocket
    

    WSS和WS之间的关系就像HTTPS和HTTP一样。

    ws和wss

    现在它可以自动识别WS和WSS

    以爬取BG站https://eosflare.io/为例:
    import asyncio
    import logging
    from datetime import datetime
    from aiowebsocket.converses import AioWebSocket
    import json
    
    
    async def startup(uri):
        data = ["message", {
            "_url": "/chain/get_token_holder_ranks",
            "_method": "POST",
            "_headers": {"content-type": "application/json"},
            "contract": "bgbgbgbgbgbg",
            "symbol": "BG",
            "page": 0,
            "limit": 50,
            "sortBy": "balance",
            "ascending": False,
            "lang": "zh-CN"
        }]
        message = json.dumps(data, ensure_ascii=False)
        async with AioWebSocket(uri) as aws:
            converse = aws.manipulator
            message = '42' + message  # 要发送的数据
            while True:
                await converse.send(message)
                print('{time}-Client send: {message}'
                      .format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message=message))
                mes = await converse.receive()
                print('{time}-Client receive: {rec}'
                      .format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))
    
    
    if __name__ == '__main__':
        remote = 'wss://api-v1.eosflare.io/socket.io/?EIO=3&transport=websocket'
        # remote = 'ws://echo.websocket.org'
        try:
            asyncio.get_event_loop().run_until_complete(startup(remote))
        except KeyboardInterrupt as exc:
            logging.info('Quit.')
    

    参考连接:

    https://github.com/asyncins/aiowebsocket

    cs