const scrapePage = async (browser, url, isToday) => { let result = {} const page = await browser.newPage() await page.goto(url) const weatherTable = await page.locator('.weather_table').waitHandle() const startTime = isToday ? new Date().getHours() + 2 : 0 const endTime = 24 for (let i = startTime; i < endTime; i++) { const selector = isToday ? `.forecast_1h[data-dialogid="${i}"] td` : `.forecast_1h[data-dialogid="${1000 + i}"] td` const fields = await weatherTable.$$(selector) const temp = await fields[2].evaluate(el => parseFloat(el.textContent)) const prec = await fields[3].evaluate(el => el.textContent) result[i] = { temp, prec } } await page.close() return result } const fetchILMeteoToday = async browser => { return await scrapePage(browser, 'https://www.ilmeteo.it/meteo/Pisa', true) } const fetchILMeteoTomorrow = async browser => { return await scrapePage( browser, 'https://www.ilmeteo.it/meteo/Pisa/domani', false, ) } const fetchILMeteoDayAfterTomorrow = async browser => { return await scrapePage( browser, 'https://www.ilmeteo.it/meteo/Pisa/dopodomani', false, ) } export default async browser => { const [today, tomorrow, dayAfterTomorrow] = await Promise.all([ fetchILMeteoToday(browser), fetchILMeteoTomorrow(browser), fetchILMeteoDayAfterTomorrow(browser), ]) return { today, tomorrow, dayAfterTomorrow, } }