Created
August 16, 2022 14:15
-
-
Save Rigo85/9861f0718b0d42fe76a530a6b481500b to your computer and use it in GitHub Desktop.
Web Scraping a https://www.gob.pe/feriados para obtener los feriados aprobados por el gobierno de Perú.
This file contains 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 axios = require('axios').default; | |
const cheerio = require('cheerio'); | |
(async () => { | |
try { | |
const response = await axios.get("https://www.gob.pe/feriados"); | |
const body = await response.data; | |
const $ = cheerio.load(body); | |
const elements = $('ul > li > .holidays__list-item-date, .holidays__list-item-name').map((i, e) => e.children[0].data); | |
const partialHolidays = Array | |
.from(Array(elements.length).keys()) | |
.filter(i => !(i % 3)) | |
.map(i => ({ | |
date: createDate(elements[i], elements[i + 1]), | |
partial_date: elements[i], | |
month: elements[i + 1], | |
motive: elements[i + 2], | |
kind: elements[i + 2].includes("público") ? "public sector" : "all sectors" | |
})) | |
; | |
const nextHolidayPartialDate = $(".holidays__recent-holiday-date").text(); | |
const nextHolidayMotive = $(".holidays__recent-holiday-name").text(); | |
const [nextPartialDate, nextPartialMonth] = nextHolidayPartialDate.split(" de ").map(str => capitalize(str)); | |
const nextHoliday = { | |
date: createDate(nextPartialDate, nextPartialMonth), | |
partial_date: nextPartialDate, | |
month: nextPartialMonth, | |
motive: nextHolidayMotive, | |
kind: nextHolidayMotive.includes("público") ? "public sector" : "all sectors" | |
}; | |
console.info([nextHoliday, ...partialHolidays]); | |
} catch (error) { | |
console.error(error); | |
} | |
})(); | |
function createDate(partialDate, stringMonth) { | |
const monthStrToNum = { | |
Enero: "01", | |
Febrero: "02", | |
Marzo: "03", | |
Abril: "04", | |
Mayo: "05", | |
Junio: "06", | |
Julio: "07", | |
Agosto: "08", | |
Septiembre: "09", | |
Octubre: "10", | |
Noviembre: "11", | |
Diciembre: "12" | |
}; | |
const day = partialDate.replace(/Lunes|Martes|Miércoles|Jueves|Viernes|Sábado|Domingo/g, "").trim(); | |
const year = new Date().getFullYear(); | |
const month = monthStrToNum[stringMonth.replace(/:/, "").trim()]; | |
return `${year}-${month}-${day}`; | |
} | |
function capitalize(word) { | |
const lower = word.toLowerCase(); | |
return word.charAt(0).toUpperCase() + lower.slice(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment