How to capture background requests and responses in Puppeteer?

You can use the page.on() function to capture the background requests and responses that go in the background when a request is made.

For example, to capture the background requests of ScrapingBee's home page, you can use this code:

const puppeteer = require('puppeteer')
try {
    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        var requests = [];
        var responses = [];

        page.on('request', request => {
            requests.push(request);
        });

        page.on('response', response => {
        responses.push(response);
        });
        await page.goto('https://scrapingbee.com');
        await browser.close();
        console.log(requests);
        console.log(responses)
    })()
} catch (err) {
    console.error(err);
}

Related Puppeteer web scraping questions: