2017-12-06 8 views
0

puppeteerは、新しいページインスタンスで複数のリンクをたどって同時並行で非同期的に評価するにはどうすればよいですか?Puppeteerを使用した並行ページの廃棄

+0

あなたは何をまで試すんでしたが尋ねているので

const pool = new PromisePool(promiseProducer, CONCURRENCY); await pool.start(); 

今?何が働いていて何がうまくいかないの? –

答えて

1

ほとんどすべての人形メソッドは、Promiseを返します。たとえば、https://www.npmjs.com/package/es6-promise-poolパッケージを使用することができます。

まず、1つのURLを処理非同期関数を作成必要があります。

const crawlUrl = async (url) => { 
    // Open new tab. 
    const page = await browser.newPage(); 
    await page.goto(url); 

    // Evaluate code in a context of page and get your data. 
    const result = await page.evaluate(() => { 
     return { 
      title: document.title, 
      url: window.location.href, 
     }; 
    }); 
    results.push(result); 

    // Close it. 
    await page.close(); 
}; 

次にあなたがプロデューサーを約束する必要があります。この関数が呼び出されるたびに、URLS_TO_BE_CRAWLEDから1つのURLをとり、crawlUrl(url)を返します。 URLS_TO_BE_CRAWLEDが空になると、代わりにnullが返され、プールが終了します。

const promiseProducer =() => { 
    const url = URLS_TO_BE_CRAWLED.pop(); 

    return url ? crawlUrl(url) : null; 
}; 

最後に、あなたの好みの並行処理でこれを実行します。これは非常に頻繁に質問私はまた私達のApifyプラットフォームで作業例を作っhttps://www.apify.com/mtrunkat/puppeteer-promise-pool-example

関連する問題