Z
Z
zlodiak2020-02-29 17:06:13
JavaScript
zlodiak, 2020-02-29 17:06:13

How to close the parser browser?

For parsing page menu links, I use the puppeteer framework. As a result, links are printed to the console, but after that, the following error message is displayed in the console:

(node:24126) UnhandledPromiseRejectionWarning: Error: Protocol error (Target.closeTarget): Target closed.
    at /home/md/.MINT18/code/js/puppeteer_books_1/node_modules/puppeteer/lib/Connection.js:74:56
    at new Promise (<anonymous>)
    at Connection.send (/home/md/.MINT18/code/js/puppeteer_books_1/node_modules/puppeteer/lib/Connection.js:73:12)
    at Page.close (/home/md/.MINT18/code/js/puppeteer_books_1/node_modules/puppeteer/lib/Page.js:1040:38)
    at Page.<anonymous> (/home/md/.MINT18/code/js/puppeteer_books_1/node_modules/puppeteer/lib/helper.js:112:23)
    at getSectionsLinks (/home/md/.MINT18/code/js/puppeteer_books_1/index.js:18:18)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:24126) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:24126) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


The reason for this error is in the line that closes the browser instance after parsing is completed:
browser.close();

Please help to safely close the browser after parsing. Here is the complete parser code:
const puppeteer = require('puppeteer');

const getSectionsLinks = async () => {
    const browser = await puppeteer.launch();
    const sectionsPage = await browser.newPage();			
    await sectionsPage.goto('https://grifbook.ru/products');

    const sectionLinks = await sectionsPage.evaluate(() => {
    	const links = [];
        const punkts = document.querySelectorAll('.catalog-menu .catalog-menu__item  a');
        punkts.forEach(punkt => {
        	links.push(punkt.getAttribute('href'));
        });
        return links;
    });

    sectionsPage.close(); 
    browser.close();
  return sectionLinks;
};


getSectionsLinks().then((sectionLinks) => {
    console.log(sectionLinks);
}).catch(function (error) {
    console.log("getSectionsLinks error", error);
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2020-02-29
@zlodiak

await    sectionsPage.close(); 
await    browser.close();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question