web-scraping - Puppeteer/Node - Target.createTarget - 目标关闭

标签 web-scraping puppeteer

我在下面的代码中使用了 Node/Puppeteer,传递了一个大的 URL 列表用于遍历和抓取。很难异步地做到这一点,尽管我发现我越来越接近答案了。我目前遇到与以下错误相关的问题。

UnhandledPromiseRejectionWarning:未处理的 promise 拒绝(拒绝 ID:17):错误:协议(protocol)错误(Target.createTarget):目标已关闭。

此错误在 while 循环的每次迭代中发生一次。虽然我不确定我可能做错了什么。

谁能帮我做以下事情:
1) 诊断错误的来源。
2) 可能会找到一种更有效的方式来异步遍历大量 URL。

async function subProc(list, batchSize) {
let subList = null;
let i = 0;

while (list.length > 0) {
    let browser = await puppeteer.launch();
    subList = list.splice(0, batchSize);
    console.log("Master List Size :: " + list.length);
    console.log("SubList Size :: " + subList.length);

    for (let j = 0; j < subList.length; j++) {
        promiseArray.push(new Promise((resolve, reject) => {
            resolve(pageScrape(subList[j], browser));
        }));
    }
    Promise.all(promiseArray)
        .then(response => {
            procArray.concat(response);
        });
    promiseArray = new Array();
    try {
        await browser.close();
    } catch(ex){
        console.log(ex);
    }
};
}

async function pageScrape(url, browser) {
let page = await browser.newPage();
await page.goto(url, {
    timeout: 0
});
await page.waitFor(1000);
return await page.evaluate(() => {
    let appTitle = document.querySelector('').innerText;
    let companyName = document.querySelector('').innerText;
    let dateListed = document.evaluate("", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerText;
    let category = document.evaluate("']//a//strong", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerText;
    /*  */
    return {
        appTitle,
        companyName,
        dateListed,
        category
    }
}).then(response => {
    let urlData = {
        id: subList[j],
        appName: response.appTitle,
        companyName: response.companyName,
        dateListed: response.dateListed,
        category: response.category
    }
    return urlData;
});

};

最佳答案

我找到了我遇到的问题的解决方案。 每台计算机的处理能力都是有限的,因此您不能同时遍历 1000 个网址,而必须将其分解成更小的部分。

通过使用 PromiseAll,一次迭代和抓取 10 个 url 并将这些值存储在一个数组中,我能够限制迭代所有 1000 个 url 所需的处理。

 processBatch(subData, 10, procArray).then((processed)=>{
            for(let i = 0; i < procArray.length; i++){
                for(let j = 0; j < procArray[i].length; j++){
                   results.push(procArray[i][j]);
                }
            }

 function processBatch(masterList, batchSize, procArray){
    return Promise.all(masterList.splice(0, batchSize).map(async url => 
    {
    return singleScrape(url)
    })).then((results) => {
    if (masterList.length < batchSize) {
        console.log('done');
        procArray.push(results);
        return procArray;
    } else {
        console.log('MasterList Size :: ' + masterList.length);
        procArray.push(results);
        return processBatch(masterList, batchSize, procArray);
    }
   })
 }

关于web-scraping - Puppeteer/Node - Target.createTarget - 目标关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705697/

相关文章:

javascript - 如何在 Puppeteers .evaluate() 方法中传递函数

Python bs4 : How to Repeat “For” Loop with a Different Scraped Page if a Certain Condition is Met?

node.js - Docker Node :Alpine-12: how to install Chromium 73 in Dockerfile?

dart - 使用 puppeteer 获取源 html

python - 如何使用 scrapy 从具有数据库的网页中提取数据

javascript - 一种防止在 puppeteer 实例中打开开发工具的方法

node.js - 使用 puppeteer 在 headless chrome 中运行 Flash 游戏

python - BeautifulSoup - 从页面中抓取多个表?

r - 如何在 Rselenium 中释放按键

web-scraping - 如何人为地创建ConnectionRefusedError?