puppeteer获取网页中的websocket数据

截止目前,Puppeteer 没有提供原生的用于处理 WebSocket 的 API 接口。只能通过更底层的 Chrome DevTool Protocol (CDP) 协议获得。

Puppeteer 使用 CDPSession 对象处理 CDP 协议相关

实现如下

const puppeteer = require('puppeteer');

(async () => {
    browser = await puppeteer.launch()
    page = await browser.newPage()
    const client = await page.target().createCDPSession()
    await client.send('Network.enable')

    client.on('Network.webSocketCreated',
        function (params) {
            console.log(`创建 WebSocket 连接:${params.url}`)
        }
    )
    client.on('Network.webSocketClosed',
        function (params) {
            console.log(`WebSocket 连接关闭`)
        }
    )
    client.on('Network.webSocketFrameSent',
        function (params) {
            console.log(`发送 WebSocket 消息:${params.response.payloadData}`)
        }
    )
    client.on('Network.webSocketFrameReceived',
        function (params) {
            console.log(`收到 WebSocket 消息:${params.response.payloadData}`)
        }
    )
    client.on('Network.webSocketWillSendHandshakeRequest',
        function (params) {
            console.log(`准备发送 WebSocket 握手消息`)
        }
    )
    client.on('Network.webSocketHandshakeResponseReceived',
        function (params) {
            console.log(`接收到 WebSocket 握手消息`)
        }
    )
    // 开始浏览
    var url = "https://xxx:9900/index.html"
    await page.goto(url)
})()

参考:
https://www.lfhacks.com/tech/puppeteer-websocket/

你可能感兴趣的:(puppeteer获取网页中的websocket数据)