这是我的代码:
const puppeteer = require('puppeteer');
const DOG_NAME = 'section.adopt.yd-amuta > div.container > div.row.yd-dog-count > div.col-xs-12 > div.adopt-head > div.row> div.col-sm-6.col-xs-12 > div.adopt-breadcrumb-title > h2 > span';
let names = [];
async function run() {
let browser = await puppeteer.launch({
headless: false,});
let pages = await browser.pages();
let num =1;
for(; num < 6329; num++){
await pages[0].goto('http://www.yad4.co.il/dogs#' + num);
let dogCard = await pages[0].$$('.col-md-4.col-sm-4.col-xs-6');
for (let i = 0; i < dogCard.length; i++) {
await pages[0].waitForSelector('.col-md-4.col-sm-4.col-xs-6');
dogCard[i].click();
}
console.log('Finished clicking.');
let count = 0;
while (count < dogCard.length) {
pages = await browser.pages();
count = pages.length;
}
for (let i = 1; i < dogCard.length; i++) {
let name = await pages[i].evaluate((sel) => {
return document.querySelector(sel).innerHTML;
},DOG_NAME);
names.push(name);
await pages[i].close();
}
for (let i = 0; i < names.length; i++) {
console.log(`${i + 1}` + ':' + `${names[i]}`);
}
if(num == 6329 ){
browser.close();
}
}
}
run();
我收到此错误:
“ UnhandledPromiseRejectionWarning:错误:执行上下文为 被摧毁,很可能是因为导航。 在rewriteError(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ ExecutionContext.js:167:15) 在process._tickCallback(内部/进程/next_tick.js:68:7)-ASYNC- 在ExecutionContext中。 (C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ helper.js:111:15) 在ElementHandle.evaluate(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ JSHandle.js:55:42) 在ElementHandle。 (C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ helper.js:112:23) 在ElementHandle._scrollIntoViewIfNeeded(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ JSHandle.js:181:30) 在ElementHandle.click上(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ JSHandle.js:282:16) 在ElementHandle。 (C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ helper.js:112:23) 运行时(C:\ Users \ Shani Balas \ Desktop \ scraping \ appnew.js:20:16) 在process._tickCallback(内部/进程/next_tick.js:68:7)(节点:19268)UnhandledPromiseRejectionWarning:未处理的承诺 拒绝。该错误是由抛出异步内部引起的 没有捕获块或拒绝承诺 未使用.catch()处理。 (拒绝ID:1)(节点:19268)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 使用非零退出代码的Node.js进程。 (节点:19268) UnhandledPromiseRejectionWarning:错误:协议错误 (Runtime.callFunctionOn):目标已关闭。 在Promise(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ Connection.js:183:56) 在新的Promise() 在CDPSession.send(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ Connection.js:182:12) 在ExecutionContext._evaluateInternal(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ ExecutionContext.js:107:44) 在ExecutionContext.evaluateHandle(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ ExecutionContext.js:57:17) 在ExecutionContext中。 (C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ helper.js:112:23) 在WaitTask.rerun(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ DOMWorld.js:570:65) 在process._tickCallback(内部/进程/next_tick.js:68:7)-ASYNC- 在帧。 (C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ helper.js:111:15) 在Page.waitForSelector(C:\ Users \ Shani Balas \ node_modules \ puppeteer \ lib \ Page.js:1125:29) 运行时(C:\ Users \ Shani Balas \ Desktop \ scraping \ appnew.js:19:20) 在process._tickCallback(内部/进程/next_tick.js:68:7)(节点:19268)UnhandledPromiseRejectionWarning:未处理的承诺 拒绝。该错误是由抛出异步内部引起的 没有捕获块或拒绝承诺 未使用.catch()处理。 (拒绝ID:2)“
你认为我应该怎么做?