Instantly share code, notes, and snippets.

Embed
What would you like to do?
const request = require('request-promise-native');
const cheerio = require('cheerio');
const scrap = async (url, selector) => {
let html;
try {
html = await request(url);
} catch (err) {
console.error(`Url no encontrada: ${url}`);
return '';
}
const $ = cheerio.load(html);
const valor = $(selector).text().trim();
return valor;
};
const URL_DOLAR_SUNAT = 'http://www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias';
const CSS_SELECTOR = '[class=\'class="form-table"\'] tr:last-child td:nth-last-child(2)';
const cambioDolarDia = async () => {
let valor = await scrap(URL_DOLAR_SUNAT, CSS_SELECTOR);
valor = parseFloat(valor);
valor = (Math.trunc(valor * 10) / 10) - 0.2;
return valor;
};
(async () => {
console.log(await cambioDolarDia());
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment