WSS协议网页爬取(状态码101)

这里用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值