[英]puppeteer-cluster, different data to the same url
我在下面举了一个例子,我想同时从 object 的数组添加不同的搜索输入(firstWord + scndWord)到两个谷歌页面,所以打开页面动态取决于数组长度第一页打开谷歌然后写红花第二页打开谷歌但写“游戏PC
const { Cluster } = require('puppeteer-cluster');
(async () => {
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_PAGE,
maxConcurrency: 10,
puppeteerOptions: {
headless: false,
},
});
cluster.on('taskerror', (err, url) => {
console.error((new Date()).toJSON() + ` Error crawling ${url}: ${err.message}`);
});
const arr = [
{firstWord: 'gaming ',scndWord: 'pc'},
{firstWord: 'red ', scndWord: 'flower'}
]
await cluster.task(async ({ page, data: index }) => {
await page.goto('https://www.google.com/');
await page.focus('body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input')
await page.keyboard.type('flower');
await page.waitForNavigation()
await page.screenshot({ path: 'screenshot.png', fullPage: true })
});
for (let index = 0; index <=arr.length -1 ; index++) {
cluster.execute(index);}
我很困惑该怎么做,我将感谢您的帮助
我知道了
const { Cluster } = require('puppeteer-cluster');
(async () => {
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_PAGE,
maxConcurrency: 10,
puppeteerOptions: {
headless: false,
},
});
cluster.on('taskerror', (err, url) => {
console.error((new Date()).toJSON() + ` Error crawling ${url}: ${err.message}`);
});
const arr = [
{firstWord: 'gaming ',scndWord: 'pc'},
{firstWord: 'red ', scndWord: 'flower'}
]
await cluster.task(async ({ page, data: [firstWord , scndWord] }) => {
await page.goto('https://www.google.com/');
await page.focus('body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input')
await page.keyboard.type(firstWord + scndWord);
await page.waitForNavigation()
await page.screenshot({ path: 'screenshot.png', fullPage: true })
});
arr.map((serach)=>{
cluster.execute([serach.firstWord, serach.scndWord]);
})
// await cluster.idle();
// await cluster.close();
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.