Estoy tratando de extraer los mismos datos de varias páginas de un sitio web. Los enlaces de URL están todos en un documento json como una matriz. No puedo averiguar cómo recorrer cada enlace del sitio web y obtener datos de cada página. Siento que necesito hacer un bucle para toda la función, pero quiero asegurarme de que todos los datos terminen en el mismo archivo. Esto es lo que tengo hasta ahora...
const fs = require('fs'); const puppeteer = require('puppeteer'); function extractItems() { const extractedElements = document.querySelectorAll('#MoreInfoPanel_81 > div.more-info-panel-body'); const items = []; for (let element of extractedElements) { items.push(element.innerText); } return items; } async function scrapeItems( page, extractItems, itemCount, scrollDelay = 800, ) { let items = []; try { let previousHeight; while (items.length < itemCount) { items = await page.evaluate(extractItems); previousHeight = await page.evaluate('document.body.scrollHeight'); await page.evaluate('window.scrollTo(0, document.body.scrollHeight)'); await page.waitForFunction(`document.body.scrollHeight > ${previousHeight}`); await page.waitForTimeout(scrollDelay); } } catch(e) { } return items; } function hospitalLinks() { let dataFile = require('./hospitallinks.json'); for (let i = 0; i < dataFile.length; i++) { } } (async () => { // Set up Chromium browser and page. const browser = await puppeteer.launch({ headless: false, args: ['--no-sandbox', '--disable-setuid-sandbox'], }); const page = await browser.newPage(); page.setViewport({ width: 1280, height: 926 }); // Navigate to the page. await page.goto(hospitalLinks()); // Auto-scroll and extract desired items from the page. const items = await scrapeItems(page, extractItems, 4908); // Save extracted items to a new file. fs.writeFileSync('./facilitydata.txt', items.join('\n') + '\n'); // Close the browser. await browser.close(); })(); Funciona si reemplazo hospitalLinks() con una URL... Intenté usar forEach pero tampoco funcionó.