简体   繁体   中英

puppeteer-cluster, different data to the same url

i put an example below that i want to add different search inputs (firstWord + scndWord) from array of object to two google pages in the same time, so opening pages dynamically depend on the array length 1st page open google then write red flower 2nd page open google but write 'gaming 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);}



        

I'm confusing how to do that, i will be thankful for the help

i got it

        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();
    })();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM