Last active
March 2, 2021 21:47
-
-
Save 1devNdogs/2dfdcc35f6e357419cdf42b39d0ed2bd to your computer and use it in GitHub Desktop.
SII Scrapper 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rp = require("request-promise"); | |
const $ = require("cheerio"); | |
const monthArray = [ | |
"#mes_enero", | |
"#mes_febrero", | |
"#mes_marzo", | |
"#mes_abril", | |
"#mes_mayo", | |
"#mes_junio", | |
"#mes_julio", | |
"#mes_agosto", | |
"#mes_septiembre", | |
"#mes_octubre", | |
"#mes_noviembre", | |
"#mes_diciembre", | |
]; | |
checkIt(); | |
async function checkIt() { | |
const thisMonth = monthArray[new Date().getMonth()]; | |
const thisYear = new Date().getFullYear(); | |
return rp({ | |
uri: "https://www.sii.cl/valores_y_fechas/uf/uf" + thisYear + ".htm", | |
}) | |
.then(function (html) { | |
let monthHtml = $(thisMonth, html).find("tbody tr"); | |
let rowDay1 = 1; | |
let rowDay2 = 11; | |
let rowDay3 = 21; | |
let finalArray = []; | |
Array.from(monthHtml).forEach((item, i) => { | |
if (i > 0) { | |
let one = $("td:nth-child(2)", item) | |
.text() | |
.replace(".", "") | |
.replace(",", "."); | |
let two = $("td:nth-child(4)", item) | |
.text() | |
.replace(".", "") | |
.replace(",", "."); | |
let three = $("td:nth-child(6)", item) | |
.text() | |
.replace(".", "") | |
.replace(",", "."); | |
if (one.length > 0) { | |
finalArray.push({ day: rowDay1, uf: parseFloat(one) }); | |
} | |
if (two.length > 0) { | |
finalArray.push({ day: rowDay2, uf: parseFloat(two) }); | |
} | |
if (three.length > 0) { | |
finalArray.push({ day: rowDay3, uf: parseFloat(three) }); | |
} | |
rowDay1++; | |
rowDay2++; | |
rowDay3++; | |
} | |
}); | |
finalArray.sort(compare); | |
console.log(finalArray); | |
}) | |
.catch(function (err) { | |
console.log(err); | |
return { err }; | |
}); | |
} | |
function compare(a, b) { | |
if (a.day < b.day) { | |
return -1; | |
} | |
if (a.day > b.day) { | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment