我使用 puppeteer-cluster + node js。我有一些脚本。我是新手
脚本运行到一半,然后结束,不再执行。我一直在寻找一个星期的解决方案,我不明白这是怎么回事。帮帮我,请在此处输入代码
const { Cluster } = require('puppeteer-cluster-delay');
(async () => {
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: 2,
puppeteerOptions: {headless: false,
},
});
await cluster.task(async ({ page, data: url }) => {
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('1');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('2');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('3');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('4');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('5');
await page.goto('http://www.nike.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('6');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('7');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('8');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('end');
});
await cluster.queue();
await cluster.queue();
// many more pages
await cluster.idle();
await cluster.close();
})();
脚本到达第 5 次迭代并结束 ((