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