Nicky
Nicky

Reputation: 57

How can I log the body of the response of a network request using puppeteer?

I am trying to make it log the data found from inspect>network>preview but right now it logs inspect>network>headers. Here is what I have:

    const puppeteer = require("puppeteer");
    
    const url =
      "https://www.google.com/";
    
    async function StartScraping() {
      await puppeteer
        .launch({
          headless: false,
        })
        .then(async (browser) => {
          const page = await browser.newPage();
    
          await page.setViewport({
            width: 1500,
            height: 800,
          });
    
          page.on("response", async (response) => {
    
    
            if (response.url().includes("Text")) {
                console.log(await response);
              }
    
          });
    
          await page.goto(url, {
            waitUntil: "load",
            timeout: 0,
          });
        });
    }
    StartScraping();

Upvotes: 0

Views: 3344

Answers (1)

OlijslagersM
OlijslagersM

Reputation: 21

It depends how you want it formatted. More information can be found here: https://github.com/puppeteer/puppeteer/blob/9ef4153f6e3548ac3fd2ac75b4570343e53e3a0a/docs/api.md#class-response

I've modified your code a bit to where I think you would want the response:

page.on("response", async (response) => {

    if (response.url().includes("Text")) {
        console.log(await response.text());
    }

});

Upvotes: 2

Related Questions