Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get body / json response from XHR request with Puppeteer

I want to get the JSON data from a website I'm scraping with Puppeteer, but I can't figure how to get the body of the request back. Here's what I've tried:

const puppeteer = require('puppeteer')
const results = [];
(async () => {
    const browser = await puppeteer.launch({
        headless: false
    })
    const page = await browser.newPage()
    await page.goto("https://capuk.org/i-want-help/courses/cap-money-course/introduction", {
        waitUntil: 'networkidle2'
    });

    await page.type('#search-form > input[type="text"]', 'bd14ew')  
    await page.click('#search-form > input[type="submit"]')

    await page.on('response', response => {    
        if (response.url() == "https://capuk.org/ajax_search/capmoneycourses"){
            console.log('XHR response received'); 
            console.log(response.json()); 
        } 
    }); 
})()

This just returns a promise pending function. Any help would be great.

like image 672
Rusty Avatar asked Jun 20 '19 15:06

Rusty


People also ask

How to convert JSON XHR response text to an object?

Because the xhr.responseText is a string, you can easily manipulate it. To work with the data, you need to convert it back into an object. You can do this using the JSON.parse () method. Here it is in full context again.

How do I get JSON from a Mongo collection in XHR?

Inside the if block, parse the response object from the request to a JSON to get the JSON of your Mongo collection and assign it to your state variable. This is how you make an XHR request to get JSON of your Mongo collection and store it inside a local state of your component.

How do I make an XHR request in react?

Making an XHR Request Inside your React app, import useState and useEffect. The useState hook is used to create a state where the JSON object from the endpoint is stored. Make an XHR request inside the useEffect lifecycle hook's callback function using the XMLHttpRequest object.

How do I make an XHR request using the useeffect lifecycle hook?

Make an XHR request inside the useEffect lifecycle hook's callback function using the XMLHttpRequest object. Have a look at the following code: Inside the useEffect ()' s callback function, create a new instance of the XMLHttpRequest object.


Video Answer


1 Answers

As response.json returns a promise we need to await it.

page.on('response', async (response) => {    
    if (response.url() == "https://capuk.org/ajax_search/capmoneycourses"){
        console.log('XHR response received'); 
        console.log(await response.json()); 
    } 
}); 
like image 136
hardkoded Avatar answered Sep 19 '22 05:09

hardkoded