We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Collecting Page Data

Now we have a crawler that can run efficiently, with options to tune concurrency and set a limit on the number of pages to crawl.

But you may have noticed that what the crawler is doing isn't very useful. It recursively fetches pages and... increments counters to record how many times it hit each page? What's the point of that?

Fortunately, we already wrote the function that we need in order to have our crawler collect actual data: extractPageData, from back in the first chapter.

What we need to do now is to update the pages member of our ConcurrentCrawler class, so that it's a map of URL strings to page data objects – we have an ExtractedPageData type for this – rather than to numbers of visits. i.e.,

pages: Record<string, ExtractedPageData>;

This way, when we call our crawler, it will return a data structure that tells us both what URLs it found and some key details about each of them.

Assignment

  1. const data = extractPageData(html, currentURL);
    
  2. for (const nextURL of data.outgoing_links)
    
  3. console.log("Finished crawling.");
    const firstPage = Object.values(pages)[0];
    if (firstPage) {
      console.log(
        `First page record: ${firstPage["url"]} - ${firstPage["heading"]}`,
      );
    }
    

Try running the crawler on https://learnwebscraping.dev/practice/ecommerce/. If everything looks right, then run and submit the CLI tests.