Skip to content

Instantly share code, notes, and snippets.

@wesdeveloper
Last active June 8, 2021 12:22
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 wesdeveloper/b2cf7a45ef66dee5a598cbb8e01b1fe8 to your computer and use it in GitHub Desktop.
Save wesdeveloper/b2cf7a45ef66dee5a598cbb8e01b1fe8 to your computer and use it in GitHub Desktop.
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
// Url a ser visitada
const pageToVisit = 'http://www.suapesquisa.com/geografia/siglas_estados_brasileiros.htm';
console.log('Visiting page ' + pageToVisit);
request(pageToVisit, function (error, response, body) {
if (error) {
console.log('Error: ' + error);
}
let $ = cheerio.load(body);
let estados = [];
let siglas = [];
// Trata as informaçoes e armazena no arquivo
$('.body td').each(function () {
let title = $(this).text().trim();
if (title.length > 0) {
if (title.length > 2) {
estados.push(title);
console.log(title);
} else {
siglas.push(title);
console.log(title);
}
}
});
estados.shift();
estados.shift();
const estadoFile = fs.createWriteStream('estados.txt');
estados.forEach((estado) => {
estadoFile.write(estado + '\n');
});
estadoFile.end();
const siglasFile = fs.createWriteStream('siglas.txt');
siglas.forEach((sigla) => {
siglasFile.write(sigla + '\n');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment