Created
June 7, 2022 08:40
-
-
Save ilhamgusti/cd7cba57d83010176a0d1f3b8aff3414 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
// Full TypeScript support for both puppeteer and the DOM | |
export default async ({ page }: { page: Page }) => { | |
// Full puppeteer API is available | |
const url = 'https://holidayapi.com/countries/id/2025'; | |
await page.goto(url, {waitUntil: 'networkidle0'}); | |
const data = await page.evaluate(()=>{ | |
const query = (selector, context) => Array.from(context.querySelectorAll(selector)); | |
const result = query('tr', document).map(row => query('td, th', row).map(cell => cell.innerText + " " + (cell.getAttribute('style')?.includes("font-weight: 900;") ? "[RED]":''))) | |
const [dateKey,dayKey,labelKey,noteKey] = result[0]; | |
const datas = result.slice(1) | |
const json = datad.map(([date,day,label,note]) =>{ | |
return { | |
[dateKey.trim().toLowerCase()]:date.replace(" [RED]",""), | |
[dayKey.trim().toLowerCase()]:day.replace(" [RED]",""), | |
national_holiday: day.includes("[RED]"), | |
[labelKey.trim().toLowerCase()]:label.replace(" [RED]",""), | |
[noteKey.trim().toLowerCase()]:note, | |
} | |
}) | |
return {result,keys:result[0],datas,json} | |
}) | |
// Logs show up in the browser's devtools | |
console.log(`I show up in the page's console!`); | |
console.log({data}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment