#!/usr/bin/env node
// vim: set noexpandtab tabstop=2:
const puppeteer = require('puppeteer');
const fs = require('fs').promises;
const cookies_json_file = process.argv[2];
const url = process.argv[3];
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const cookiesString = await fs.readFile(cookies_json_file);
const cookies = JSON.parse(cookiesString);
await page.setCookie.apply(page, cookies);
//await page.goto(url);
await page.goto(url, { waitUntil: 'networkidle2' });
const content = await page.content();
console.log(content);
await browser.close();
})();
Когда я запускаю приведенный выше код, я получил следующую ошибку. Затем он висит там без завершения.
(node:50025) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 30000ms exceeded
at /usr/local/lib/node_modules/puppeteer/lib/LifecycleWatcher.js:142:21
-- ASYNC --
at Frame.<anonymous> (/usr/local/lib/node_modules/puppeteer/lib/helper.js:110:27)
at Page.goto (/usr/local/lib/node_modules/puppeteer/lib/Page.js:629:49)
at Page.<anonymous> (/usr/local/lib/node_modules/puppeteer/lib/helper.js:111:23)
at main.js:19:13
at processTicksAndRejections (internal/process/task_queues.js:89:5)
(node:50025) 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:50025) [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.
Хотя есть обсуждение этого сообщения, я не могу следить за ним и не могу понять, как изменить этот код. Кто-нибудь может показать мне, как исправить этот код? Спасибо.
https://thecodebarbarian.com/unhandled-promise-rejections-in-node.js.html
РЕДАКТИРОВАТЬ: вот код обновления до сих пор.
#!/usr/bin/env node
// vim: set noexpandtab tabstop=2:
const puppeteer = require('puppeteer');
const fs = require('fs').promises;
const cookies_json_file = process.argv[2];
const url = process.argv[3];
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const cookiesString = await fs.readFile(cookies_json_file);
const cookies = JSON.parse(cookiesString);
await page.setCookie.apply(page, cookies);
try {
await page.goto(url, { waitUntil: 'networkidle2' });
const content = await page.content();
console.log(content);
} catch (e) {
console.error(e);
process.exit(1);
} finally {
await browser.close();
}
})();