Skip to content

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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.