puppeteer-cluster, different data to the same url

261 views Asked by At

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

1

There are 1 answers

1
med amine On

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