How to scrap data using Node JS

Sometimes we need data for testing purposes or any purposes and as a developer, you may not going to enter all the data manually. Instead of doing manually how about take data from any running website. Wait, what? without accessing the database how can somebody take data from a running website which is also not owned by you? Yes, it’s possible and that is what called scrapping. Let’s see then how to scrap data using Node JS.

In this article, we are going to scrap mobile phone data from For reference, I am using the below link. If it’s broken in the future please comment in the below comment box, I will fix it.


Node JS

If you dont have Node JS setup follow Install NodeJS in Windows/Linux/macOS.

Project Setup

To setup the project open your terminal and follow the below command

mkdir amazon-scrapper

cd amazon-scrapper

npm init  //this will ask some questions just follow along.

npm install x-ray

touch index.js

After completion of above commands , we will be ready to start the code part.

Code time

So, In the previous step, we installed x-ray library from npm. This library will help us to scrap data from

To begin code part, open index.js file and these codes in that file.

Step 1 – import required libraries.

const fs = require('fs');
const XRay = require('x-ray');

fs is needed because once our scrapping is done we will save that data in a file. and X-ray is needed for scrapping.

Step 2 – Instantiate X-ray library to use

const x = XRay();

Step 3 – add scrapping functionalities.

const url =

x(url, '.s-result-list > li', [
		path: '.a-link-normal@href',
		title: 'a h2',
		price: '.s-price',
	.then(res => {

		fs.writeFileSync('data.json', JSON.stringify(res, null, 2));
	.catch(err => {
		console.log('ERROR', err);

You can see in x() function I am passing few parameters.

First parameter : URL of website from where we are scrapping data.

Second parameter: Parent DOM classes followed by child DOM li of list.

Third parameter: It is an array of objects with key and value. The key can be anything but the value is Children DOM of the second parameter where our actual data exists. You can inspect and see their DOM on the given link.

Step 4 – Execute / Run the index.js file

type the following command on terminal and will create data.json file with the information we needed in JSON format.

node index.js

So that’s all about how to scrap data using Node JS. Hope you liked this article. If you getting any difficulties let me know in the below comment box.