Covid-19 cases update using Cheerio Library
source link: https://www.geeksforgeeks.org/covid-19-cases-update-using-cheerio-library/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Covid-19 cases update using Cheerio Library
In this article we are going to learn about that how can we get the common information from the covid website i.e Total Cases, Recovered, and Deaths using the concept of scraping with help of JavaScript Library.
Library Requirements and installation: There are two libraries that are required to scrap(or get the required information from the website) the covid-19 website:
Request Library: This Request library will help us to get the HTML of the website by sending the HTTP request to that website. Because of this library, our manual tasks reduce.
Installation:
npm install request
Syntax:
request(url of website, function to get the response)
Note: The second parameter which is the function indicates that you can get the HTML, body, head, etc as a result by sending the request to a website.
Cheerio Library: This Cheerio library is used to wrap by obtaining the required data either in form of array type or individual data from the HTML that we had got by using the request library.
Installation:
npm install cheerio
Syntax:
cheerio.load(response on request)
Note: Load the HTML or any code that you had got as a response and then get the required data by passing the specific className or idName
Example: Let’s see the implementation to obtain the required data from the covid-19 website as explained below:
- Javascript
const request = require( "request" ); const cheerio = require( "cheerio" ); // requesting the covid-19 website function cb(err, response, html) { if (err) { console.log(err); } else { handlehtml(html); // Getting the html on request } } function handlehtml(html) { // loading the html that we had obtained by requesting let seltools = cheerio.load(html); // Obtaining the required data from // html which is stored in the seltools let content = seltools( "#maincounter-wrap span" ); let obj = [ "Coronavirus Cases: " , "Deaths: " , "Recovered: " ]; for (let i = 0; i < content.length; i++) { let data = seltools(content[i]).text(); console.log(obj[i] + data); } } |
Step to run the application: Write the below step in the terminal to run the application.
node fileName.js
Output:
Output
Explanation: First we requested the covid-19 website to get the HTML of that by using the request library and then we loaded the HTML by using the cheerio library to obtain the required data from that HTML by using the class name of the element in which the data was stored and as a result, we got the total cases, recovered and deaths as an output as you see above.
“This course was packed with amazing and well-organized content! The project-based approach of this course made it even better to understand concepts faster. Also the instructor in the live classes is really good and knowledgeable.”- Tejas | Deutsche Bank
With our revamped Full Stack Development Program: master Node.js and React that enables you to create dynamic web applications.
So get ready for salary hike only with our Full Stack Development Course.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK