DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Strengthening Web Application Security With Predictive Threat Analysis in Node.js
  • Unlocking the Power of Streaming: Effortlessly Upload Gigabytes to AWS S3 With Node.js
  • How To Obtain IP Geolocation Data in Next.js
  • Creating a Polar Chart to Measure Electromagnetic Field Strength

Trending

  • A Software Factory on Kubernetes: Gemini Builds, jev Decides (Genkit + senro in Go)
  • Decoupling a Data Pipeline From the Tables It Was Never Supposed to Depend On
  • Event Sourcing in Distributed Systems: Architecture, Consistency, and Fault-Tolerant Projections
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. How to Scrape E-Commerce Data With Node.js and Puppeteer

How to Scrape E-Commerce Data With Node.js and Puppeteer

Normalized data is the foundation for all price intelligence projects. This tutorial will cover the basics of how to scrape product information.

By 
Andreas Altheimer
·
Updated Jan. 15, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Web scraping is nothing new. However, the technologies that are used to build websites are constantly developing. Hence, the techniques that have to be used to scrape a website have to adapt.

Why Node.js?

A lot of websites use front-end frameworks like React, Vue.js, Angular, etc., which load the content (or parts of the content) after the initial DOM is loaded. This especially applies to performance-optimized e-commerce websites, where price and production information are loaded asynchronously.

Now, if we access a page like this with PHP, or any other classic server-side language, this content will not be part of the retrieved markup, as we require a browser window for sufficient JavaScript rendering.
This is where Puppeteer comes in. It opens a headless Chrome instance to render a page.

Getting Started – Prerequisites

Let us get started by installing Node.js on our system by initializing a new npm (Node Package Manager) instance. npm allows us to install further packages easily. To begin, run the following command:

Shell
 




x


 
1
npm init
2

          
3
// we can now install our puppeteer instance via npm
4
npm install puppeteer


With this, we have initialized a new npm instance and installed our headless Chrome browser. At this point, you could also install a DOM parser library to make data extraction a little easier. However, we are going to use the JavaScript built-in querySelector() to parse retrieved HTML.

That's it. We are finished with all the prerequisites. Let's start working on our actual web scraper.

Building the Scraper

Let us create a new file, called index.js and start by importing the previously installed Puppeteer library. 

JavaScript
 




x


 
1
const puppeteer = require ('puppeteer');
2

          
3
puppeteer.launch().then (async browser => { 
4
  const page = await browser.newPage ();    
5
  await page.goto ('https://www.rentomojo.com/noida/furniture/rent-hutch-wardrobe-2-door');     
6
  await page.waitForSelector ('.price-box__price');
7

          


Next, we launch a new headless Chrome window. The await command tells Puppeteer to wait to proceed to the next line until the related statement is completed. Following this pattern, we open up our e-commerce site and tell the browser to wait until the element that contains all information that we want to scrape is visible. 

In our case, this element is a div-container, labeled with the price-box__price class. Now the website is in the state where all information that is relevant to us is visible. 

As a next step, we are going to use a function called evaluate(). It allows us to interfere with the rendered website, which is what we need to do if we want to scrape it.

JavaScript
 


x
 
1
let priceInformation = await page.evaluate (() => {
2
  
3
  let amount = document.body.querySelector('.price-box__amount');
4
  let currency = document.body.querySelector('.price-box__rupee-sign');
5
  
6
  let productInfo = {
7
    amount: amount ? amount : null,
8
    currency: currency ? currency : null
9
  };
10
  
11
  return productInfo;
12
});
13
14
console.log(priceInformation);

In the code snippet above, we first select the desired product information and save them into the variables amount and currency. Next, we save them to an object and declare null as a fallback value in case the property does not exist.

The console.log() statement will return the gathered information, as shown in the screenshot below:

We now want to get rid of the linebreaks and all spacing. Additionally, we want to convert the property amount to an integer value. 

This is the complete and finalized code snippet:

JavaScript
 




xxxxxxxxxx
1
33


 
1
const puppeteer = require('puppeteer');
2

          
3
puppeteer.launch().then(async browser => {
4
    const page = await browser.newPage();
5
    await page.goto('https://www.rentomojo.com/noida/furniture/rent-hutch-wardrobe-2-door');
6
    await page.waitForSelector('.price-box__price');
7

          
8

          
9
    let priceInformation = await page.evaluate(() => {
10

          
11
        let amount = document.body.querySelector('.price-box__amount');
12
        let currency = document.body.querySelector('.price-box__rupee-sign');
13

          
14
        function stripString(rawString) {
15
            return rawString.trim();
16
        }
17

          
18
        let productInfo = {
19
            amount: amount ? parseInt(stripString(amount.textContent)) : null,
20
            currency: currency ? stripString(currency.textContent) : null
21
        };
22

          
23
        return productInfo;
24
    });
25

          
26
    // Logging the results
27
    console.log(priceInformation);
28

          
29
    // Closing the browser instance
30
    await browser.close ();
31

          
32
});


This will output the data in an expected and well-structured way:

Of course, you would want to scrape a lot more data properties when applying and running this in a serious web project. But this tutorial is about the concept behind it.

This article was used as a basis for the creation of this article. 

Node.js Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Strengthening Web Application Security With Predictive Threat Analysis in Node.js
  • Unlocking the Power of Streaming: Effortlessly Upload Gigabytes to AWS S3 With Node.js
  • How To Obtain IP Geolocation Data in Next.js
  • Creating a Polar Chart to Measure Electromagnetic Field Strength

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook