0

Ich habe ein Spiel mit Nodejs und die Art, wie wir jetzt einen kopflosen Chrom-Browser haben und damit interagieren können, was ziemlich toll ist!Headless chrome nodejs async loop für scraping

Ich habe etwas Code und ich habe es für das Scraping 1 Website ohne Probleme arbeiten. Allerdings, wenn ich mehrere Male kratzen möchte, scheint meine Schleife es durcheinander zu bringen und ich bin mir ziemlich sicher, dass es alles mit Async zu tun hat.

Meine Schleife ist in der Nähe der Unterseite dieses Codes - hat jemand irgendwelche Vorschläge?

Vielen Dank!

const HeadlessChrome = require('simple-headless-chrome') 

const browser = new HeadlessChrome({ 
    headless: true, // If you turn this off, you can actually see the browser navigate with your instructions, 
}) 

async function navigateWebsite(urlToGoTo) { 
    try { 
    await browser.init() 

    const mainTab = await browser.newTab({ 
     privateTab: false 
    }) 

    await mainTab.inject('jquery') 

    let cookieName = 'li_at' 
    let cookieValue = 'cyzzzzzzzzz' 
    let cookieDomain = '.www.linkedin.com' 

    await mainTab.setCookie(cookieName, cookieValue, { 
     domain: cookieDomain 
    }) 

    // Navigate to a URL 
    await mainTab.goTo(urlToGoTo) 
    await mainTab.wait(2000); 

    // Get a HTML tag value based on class id 
    let businessName = await mainTab.evaluate(function (selector) { 
     const selectorHtml = document.querySelector(selector) 
     return selectorHtml.innerHTML 
    }, '.org-top-card-module__name'); 

    let industry = await mainTab.evaluate(function (selector) { 
     const selectorHtml = document.querySelector(selector) 
     return selectorHtml.innerHTML 
    }, '.company-industries'); 

    let followers = await mainTab.evaluate(function (selector) { 
     const selectorHtml = document.querySelector(selector) 
     return selectorHtml.innerHTML 
    }, '.org-top-card-module__followers-count'); 

    let details = { 
     businessName: cleanData(businessName), 
     industry: cleanData(industry), 
     followers: cleanData(followers) 
    } 

    console.log(details) 


    // Resize the viewport to full screen size (One use is to take full size screen shots) 
    await mainTab.resizeFullScreen() 

    // Take a screenshot 
    await mainTab.saveScreenshot() 

    // Close the browser 
    await browser.close() 

    } catch (err) { 
    console.log('ERROR!', err) 
    } 
} 

let websites = [] 

websites.push('https://www.linkedin.com/company/qrious-limited/') 
websites.push('https://www.linkedin.com/company/wentworth-consulting-nz-/') 
websites.push('https://www.linkedin.com/company/capita/') 

websites.forEach(function (i) { 
    navigateWebsite(i) 
}) 


function cleanData(a) { 
    return a.result.value.replace(/(\r\n|\n|\r)/gm, "").trim() 
} 

Antwort

1

navigateWebsite() ist asynchron, aber es ist nicht abgewartet. Sie können Promise.all() verwenden, um Ihre Liste von Websites Ihrer Navigationsfunktion zuzuordnen, oder stellen Sie sicher, dass jedes Ergebnis await ist.