Skip to content
Related Articles

Related Articles

How to scrape the web data using cheerio in Node.js ?

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Nov, 2021

Node.js is an open-source and cross-platform environment that is built using the chrome javascript engine. Node.js is used to execute the javascript code from outside the browser.

Cheerio: Its working is based on jQuery. It’s totally working on the consistent DOM model. Cheerio is used for scraping web data sometimes and also used for automated tasks.

Approach: In this article, we scrapped the data of world meter a covid info website where we get the total confirmed cases total number of death, and total patients who are recovered till now.

Below is the step by step implementation:

Step 1: Enter the cmd and type the below command which will create the package.json file

npm init

Step 2: After creating the package, JSON file you need to install the cheerio, request and chalk from the below command:

npm install request cheerio chalk

Step 3: Now your Project directory looks like this:

Project Structure

Step 4:Now we create the index.js file and write the below code:

index.js




const { Cheerio } = require("cheerio");
const request = require("request");
const cheerio = require("cheerio");
const chalk = require("chalk");
  
  
function cb(error, response, html) {
  if (error) {
    console.error("Error:", error);
  } else {
    handleItem(html);
  }
}
function handleItem(html) {
  let setTool = cheerio.load(html);
  let contentArr = setTool("#maincounter-wrap span");
  
  let total = setTool(contentArr[0]).text();
  let death = setTool(contentArr[1]).text();
  let recovered = setTool(contentArr[2]).text();
  
  console.log(chalk.gray("Total cases:" + total));
  console.log(chalk.red("Total Death:" + death));
  console.log(chalk.green("Total cases:" + recovered));
}


Output: Open the command prompt and enter the below command

node index.js

Output

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!