当前,我有一些代码正在从mongodb获取数组。搜寻器正在遍历整个数组,并正在从网站上针对数组中的每个值进行搜寻。我的问题是它目前真的很慢。我将如何并行化它以便同时刮取更多数据?
编辑:这是我要并行化的函数的示例:
async function scrape(browser, page, i) {
//Start new Browser session
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
await page.goto(dealer);
//Site specific code
await page.type('.Search-bar-text-input', arrayOfArticles[i]);
await page.click('.Embedded-search-button')
await page.waitFor('.BuyingOptions-total', {timeout: 2000});
price = await page.evaluate(() => {
price = document.querySelector('.BuyingOptions-total').innerText;
console.log(price);
return price;
});
答案 0 :(得分:0)
您始终可以使用child_process
或cluster
手动进行操作。或者,您可以使用更自动的node-pool
。
来自此issue:
const cluster = require("cluster"); const numCPUs = require("os").cpus().length; const headless = true; let forks = numCPUs; // 4 let browser; if (cluster.isMaster) { for (let i = 0; i < forks; i++) { cluster.fork(); console.log('Fork #' + i); } } else { connectToChrome(); } function connectToChrome() { (async function() { let headless = false; if (args.headless) { headless = true; } browser = await puppeteer.launch({headless: headless, args: ['--no-sandbox']}); })(); }