concept Cheerio in category data

This is an excerpt from Manning's book Data Wrangling with JavaScript.
Many useful libraries will help in your day-to-day coding. Underscore and Lodash come to mind. The ubiquitous JQuery seems to be going out of fashion at the moment, although it still contains many useful functions. For working with collections of data linq, a port of Microsoft LINQ from the C# language, is useful. My own Data-Forge library is a powerful tool for working with data. Moment.js is essential for working with date and time in JavaScript. Cheerio is a library for scraping data from HTML. There are numerous libraries for data visualization, including but not limited to D3, Google Charts, Highcharts, and Flot. Libraries that are useful for data analysis and statistics include jStat, Mathjs, and Formulajs. I’ll expand more on the various libraries through this book.
4.3.2 Scraping with Cheerio
We can now identify the data in the web page, and we’re ready to get into the code. If you installed all dependencies for the chapter 4 code repository, then you already have Cheerio installed. If not, you can install Cheerio in a fresh Node.js project as follows:
npm install --save cheerioCheerio is a fantastic library that is modeled on jQuery, so if you’re already comfortable with jQuery, you’ll be at home with Cheerio. Listing 4.2 is a working example that scrapes the largest earthquakes web page and extracts the embedded data to the core data representation. You can run this code, and it will print the scraped data to the console.
Listing 4.2 Importing data by scraping a web page (listing-4.2.js)
const request = require('request-promise'); #1 const cheerio = require('cheerio'); #2 function scrapeWebPage (url) { #3 return request.get(url) #4 .then(response => { #5 const $ = cheerio.load(response); #6 const headers = $("thead tr") #7 .map((i, el) => { #7 return [$(el) #7 .find("th") #7 .map((i, el) => { #7 return $(el).text(); #7 }) #7 .toArray()]; #7 }) #7 .toArray(); #7 const rows = $("tbody tr") #8 .map((i, el) => { #8 return [$(el) #8 .find("td") #8 .map((i, el) => { #8 return $(el).text(); #8 }) #8 .toArray()]; #8 }) #8 .toArray(); #8 return rows.map(row => { #9 const record = {}; #9 headers.forEach((fieldName, columnIndex) => { #9 if (fieldName.trim().length > 0) { #9 record[fieldName] = row[columnIndex]; #9 } #9 }); #9 return record; #9 }); #9 }); #5 }; #3 const url = "https://earthquake.usgs.gov/earthquakes/browse/largest-world. ➥ php"; #10 scrapeWebPage(url) #11 .then(data => { #12 console.log(data); #12 }) #12 .catch(err => { #13 console.error(err); #13 }); #13 #1 Requires request-promise. We use this to retrieve the web page to be scraped. #2 Requires Cheerio, a third-party library we use to extract data from the HTML web page #3 Defines a helper function to extract data from the web page #4 Uses request-promise to retrieved the web page through HTTP #5 Invokes a callback to handle retrieved data #6 Loads the web page’s HTML into Cheerio #7 Uses Cheerio to extract table header and convert to an array #8 Uses Cheerio to extract table rows and convert to an array #9 Takes header and data rows, combines them, and converts to CDR #10 Shows the URL of the web page that’s to be scraped #11 Invokes the helper function to scrape the example web page #12 Prints imported data to console to check it #13 Handles any errors that might have occurred