0

I'm working on my Master's thesis, that concerns Web Page Segmentation and I'm using Puppeteer to get data about visual elements in rendered web page. I've read a lot information about Puppeteer and how to assign data to variables scraped from page, but I've found no real solution for a problem.

async function process() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://en.wikipedia.org/wiki/Coronavirus');
    await page.waitForSelector('h1');

    const returned = await page.evaluate(async () => {
        const elements = Array.from(document.querySelectorAll('h1'));
        var h1Elems = elements.map(element => {
            var color = window.getComputedStyle(element, null).getPropertyValue('color');
            var bgcolor = window.getComputedStyle(element, null).getPropertyValue('background-color');
            var bbox = JSON.stringify(element.getBoundingClientRect());
            return {bbox:bbox, bgcolor:bgcolor, color:color};
        });
    
        return { h1:h1Elems };
    });

    console.log(returned); 
    browser.close();
}
process();

My question is: Is there way how to get data out of Promise, like this?

    ...
    console.log(returned); 
    browser.close();
}

var data = process().then(returned => {return returned});

I know, I can log it into console like this:

    ...
    console.log(returned); 
    browser.close();
}
process().then(returned => console.log(returned));

But I don't need it logged in console. I need as JSON to work with.

Thank you.

Tomas Zubrik
  • 347
  • 3
  • 11

0 Answers0