【问题标题】:How to handle multiple tabs in puppeteer-cluster[CONCURRENCY_BROWSER]?如何处理 puppeteer-cluster[CONCURRENCY_BROWSER] 中的多个选项卡?
【发布时间】:2022-02-26 08:27:26
【问题描述】:

我正在尝试在以下条件下抓取 3 个网址

  1. 每个网址都需要在单独的浏览器中运行。

  2. 网址可能包含 2 个或更多点击链接

  3. 在相应浏览器(并行)的新标签中打开链接并切换到它并抓取内容。

换句话说,我正在尝试在浏览器中打开一个 url,获取页面中的链接,根据在同一浏览器中获取的链接数量打开新选项卡,切换选项卡单击其中的按钮并获取确认消息.

我还需要并行运行 3 个 url。

我尝试了 CONCURRENCY_BROWSER 选项来并行运行 url,但我无法在新选项卡中打开链接。关于如何操作 puppeteer-cluster 中的选项卡的任何建议

我需要的是:

async function test(){
    const cluster = await Cluster.launch({
        puppeteerOptions: {
            headless: false,
            defaultViewport: null, 
        },
      
        concurrency: Cluster.CONCURRENCY_BROWSER,
        maxConcurrency: 5,
        skipDuplicateUrls : true,
        timeout : 240000,
    });

    // initiate the cluster task for a set of urls from the cluster queue;
    
    await page.goto(url);
    
    // on visiting the page i retrieve 2 or more links and store it in a array
    
    let linksArray = [...subUrl];
    
    //load suburl in a new tab respectively of the same browser

    await cluster.newPage()

    //screenshot suburl
    
    await page.screenshot(suburl)
        
}

TypeError: cluster.newPage 不是函数

在 puppeteer 中,我曾经使用命令打开一个新标签 等待 browser.newPage()

【问题讨论】:

    标签: javascript node.js puppeteer puppeteer-cluster


    【解决方案1】:

    puppeteer-cluster 的作者在这里。重复使用相同的浏览器并不容易。但是,您可以定义一个任务,其中包含多个 page.goto 调用,如下所示:

    const cluster = await Cluster.launch(/* ... */);
    
    // define the task and reuse the window 
    await cluster.task(async ({ page, data: url }) => {
        await page.goto(url);
        const secondUrl = /* ... */; // extract another URL somehow
        await page.goto(secondUrl);
        await page.screenshot(/* ... */);
    });
    
    // queue your initial links
    cluster.queue('http://...');
    cluster.queue('http://...');
    // ...
    

    【讨论】:

      【解决方案2】:

      这是在同一个浏览器实例上打开多个选项卡的示例

      async function init(){
      
          var  browser = await puppeteer.launch({headless: false        ,  args: [ '--no-sandbox', '--disable-setuid-sandbox' , ]});
          open_tab('http://example1.com' , browser);
          open_tab('http://example2.com' , browser);
          open_tab('http://example3.com' , browser);
      
      }
      
      
      async function open_tab( url , browser ){
      
      
          let  page  = await browser.newPage();
          await page.setViewport({width: 1200, height: 1000});
          await page.goto( url );
      
      }
      

      【讨论】:

      • 我已经用代码 sn-p 更新了问题,请查看
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 2022-07-11
      相关资源
      最近更新 更多