Skip to content

Instantly share code, notes, and snippets.

@slmcmahon
Created February 23, 2020 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slmcmahon/e9aa2baaba2c51b64482d3f8349ce780 to your computer and use it in GitHub Desktop.
Save slmcmahon/e9aa2baaba2c51b64482d3f8349ce780 to your computer and use it in GitHub Desktop.
U.S. State Codes
const axios = require('axios');
const cheerio = require('cheerio');
const url = 'https://www.infoplease.com/us/postal-information/state-abbreviations-and-state-postal-codes';
axios.get(url).then(response => {
const $ = cheerio.load(response.data);
$('table.sgmltable tr').each((i, elem) => {
const children = $(elem).children()
const stateName = $(children[0]).text();
const code = $(children[2]).text();
if (code !== 'Postal Code') {
console.log(`${code}: ${stateName}`);
}
})
}).catch(error => {
console.log(error);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment