Skip to content

Instantly share code, notes, and snippets.

@maietta
Created December 15, 2023 17:53
Show Gist options
  • Save maietta/7294791a0b2b21cd40f7d831b326f53a to your computer and use it in GitHub Desktop.
Save maietta/7294791a0b2b21cd40f7d831b326f53a to your computer and use it in GitHub Desktop.
Fetch current Representatives and save to CSV
// npm i node-fetch cheerio
// npx ts-node reps2csv.js
import fetch from 'node-fetch';
import cheerio from 'cheerio';
import fs from 'fs';
async function fetchAndParseHouseGov() {
try {
const response = await fetch('https://www.house.gov/representatives');
const html = await response.text();
const $ = cheerio.load(html);
const byStateSection = $('#by-state');
if (byStateSection.length > 0) {
const stateTables = byStateSection.find('table.table');
let csvData = 'State,District,Name,Party,Office Room,Phone,Committee Assignment\n'; // Header row for CSV
stateTables.each((index, table) => {
const stateName = $(table).find('caption').text().trim();
const rows = $(table).find('tbody > tr');
rows.each((rowIndex, row) => {
const district = $(row).find('.views-field-value-2').text().trim();
const name = $(row).find('.views-field-value-4').text().trim();
const party = $(row).find('.views-field-value-7').text().trim();
const officeRoom = $(row).find('.views-field-value-8').text().trim();
const phone = $(row).find('.views-field-value-10').text().trim();
const committee = $(row).find('.views-field-markup').text().trim();
// Append data to CSV string with state information
csvData += `"${stateName}","${district}","${name}","${party}","${officeRoom}","${phone}","${committee}"\n`;
});
});
// Write CSV data to a file
fs.writeFileSync('house_representatives.csv', csvData);
console.log('CSV file generated: house_representatives.csv');
} else {
console.error('Section "by-state" not found.');
}
} catch (error) {
console.error('Error fetching/parsing data:', error);
}
}
fetchAndParseHouseGov();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment