Puppeteer 无法捕获所有网络请求

时间:2021-06-01 15:32:42

标签: javascript networking cookies puppeteer

我正在使用 puppeteer 捕获页面中的所有网络请求并从请求中提取标头列表。我主要对广告请求感兴趣。 这是我使用的代码

        const browser = await puppeteer.launch({
        headless: true,
        defaultViewport: { width: 1600, height: 800 },
        args: ['--disable-web-security', '--disable-features=IsolateOrigins,site-per- 
        process']});
        const page = await browser.newPage();
        await page.setViewport({ width: 1600, height: 800 });
        const client = await page.target().createCDPSession();
        await client.send('Network.clearBrowserCookies');
        await client.send('Network.clearBrowserCache');
        await page.setCacheEnabled(false);
        await page.setRequestInterception(true);
        const results=[];
        page.on('request', (request) => {
            const information = {
                url: request.url(),
                headers: request.headers(),
            };

            results.push(information);
            request.continue();
        });
        page.on('requestfinished', async (request) => {
            const response = await request.response();

            const responseHeaders = response.headers();
            const headers = Array.from(request.headers());
            headers.concat(Array.from(responseHeaders));

            const information = {
                url: request.url(),
                headers,
            };
            results.push(information);
        });

        page.on('response', async (response) => {
            const information = {
                url: response.url(),
                headers: response.headers(),
            };

            results.push(information);
        });
        const url='https://www.javatpoint.com/php-tutorial';
        await page.goto(url, {
            waitUntil: 'networkidle0',
            timeout: 0,
        });

但是,与代码捕获的请求数量相比,我可以看到浏览器中的请求数量至少是其 10 倍。此外,视口从未设置为 1600*800

0 个答案:

没有答案