F
F
Fuckingawesomenigga2021-03-13 02:38:58
Parsing
Fuckingawesomenigga, 2021-03-13 02:38:58

Puppeteer | How to close the browser if the block is not found?

Now if the scrapper doesn't find the block, the browser stays open and waits.

const scraperObject = {
    async scraper(browser) {
        const urls = [
            'https://link.com'
        ];

        let pagePromise = (link) => new Promise(async (resolve, reject) => {
            let dataObj = {};
            let newPage = await browser.newPage();
            await newPage.goto(link);

            dataObj['text'] = await newPage.$eval('.block', div => div.textContent);

            resolve(dataObj);
            await newPage.close();
        });

        for (link in urls) {
            let currentPageData = await pagePromise(urls[link]);
            console.log(currentPageData)
        };
    }
}

module.exports = scraperObject;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alekssamos, 2021-03-14
@fuckingawesomenigga

1. Reading this ;
2. Or we just try to catch the error:

let newPage = await browser.newPage();
            try {
                await newPage.goto(link);
                dataObj['text'] = await newPage.$eval('.block', div => div.textContent);
            } catch(er) {
                await newPage.close();
                // await browser.close(); // для наглядности
                //console.error(er); // показать ошибку в консоле
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question