I'm trying to scrape data from a CDC website.
I'm using cheerio.js to fetch the data, and copying the HTML selector into my code, like so:
const listItems = $('#tab1_content > div > table > tbody > tr:nth-child(1) > td:nth-child(3)');
However, when I run the program, I just get a blank array. How is this possible? I'm copying the HTML selector verbatim into my code, so why is this not working? Here is a short video showing the issue: https://youtu.be/a3lqnO_D4pM
Here is my full code, along with a link were you can run the code:
const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
// URL of the page we want to scrape
const url = "https://nccd.cdc.gov/DHDSPAtlas/reports.aspx?geographyType=county&state=CO&themeId=2&filterIds=5,1,3,6,7&filterOptions=1,1,1,1,1";
// Async function which scrapes the data
async function scrapeData() {
try {
// Fetch HTML of the page we want to scrape
const { data } = await axios.get(url);
// Load HTML we fetched in the previous line
const $ = cheerio.load(data);
// Select all the list items in plainlist class
const listItems = $('#tab1_content > div > table > tbody > tr:nth-child(1) > td:nth-child(3)');
// Stores data in array
const dataArray = [];
// Use .each method to loop through the elements
listItems.each((idx, el) => {
// Object holding data
const dataObject = { name: ""};
// Store the textcontent in the above object
dataObject.name = $(el).text();
// Populate array with data
dataArray.push(dataObject);
});
// Log array to the console
console.dir(dataArray);
} catch (err) {
console.error(err);
}
}
// Invoke the above function
scrapeData();
Run the code here: https://replit.com/@STCollier/Web-Scraping#index.js
Thanks for any help.