Skip to content

Instantly share code, notes, and snippets.

@merunga
Created September 5, 2018 23:47
Show Gist options
  • Save merunga/1889ddef0f7aaff67565121ca3c08f0b to your computer and use it in GitHub Desktop.
Save merunga/1889ddef0f7aaff67565121ca3c08f0b to your computer and use it in GitHub Desktop.
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