Skip to content

Instantly share code, notes, and snippets.

@lizzybrooks
Created April 30, 2024 23:01
Show Gist options
  • Save lizzybrooks/9accaeb85751f0e2a799e6e5d430ab75 to your computer and use it in GitHub Desktop.
Save lizzybrooks/9accaeb85751f0e2a799e6e5d430ab75 to your computer and use it in GitHub Desktop.
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');
const url = 'https://www.lwhs.org/athletics/calendar-results';
// Function to fetch and scrape the website
async function scrapeData() {
try {
// Fetching HTML from the URL
const { data } = await axios.get(url);
// Load HTML into cheerio
const $ = cheerio.load(data);
// Create an array to store the scraped data
const results = [];
// Select and iterate over table rows in the tbody of the .fsEventTable
$('.fsEventTable tbody tr').each((index, element) => {
// Skip the "Load More" button row
if (!$(element).hasClass('fsLoadMoreButtonRow')) {
const row = {
team: $(element).find('.fsTitle').text().trim(),
opponent: $(element).find('.fsAthleticsOpponentName').text().trim(),
date: $(element).find('.fsAthleticsDate time').attr('datetime'),
time: $(element).find('.fsAthleticsTime time').attr('datetime'),
location: $(element).find('.fsAthleticsLocations').text().trim(),
advantage: $(element).find('.fsAthleticsAdvantage').text().trim(),
detailsLink: $(element).find('.fsAthleticsDetails a').attr('href')
};
results.push(row);
}
});
// Structure the data with a 'games' key
const jsonData = {
games: results
};
// Save results to a JSON file
fs.writeFile('athletics.json', JSON.stringify(jsonData, null, 2), (err) => {
if (err) {
console.error('Error writing to file:', err);
return;
}
console.log('Data has been saved to athletics.json');
});
} catch (error) {
console.error('Error fetching data:', error);
}
}
// Call the scrape function
scrapeData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment