Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
Axios y Cheerio: ¿Visita una lista de URL contenidas en un archivo JSON y agrega datos extraídos a ese objeto?

Estoy intentando construir un raspador con Axios y Cheerio.

Necesito hacer esto en dos partes. Ya completé la primera parte, que visita un sitio y me escribe un archivo JSON que contiene un montón de objetos como este:

 [ { "count": 0, "title": "Result One", "url": "http://www.resultone.com" }, { "count": 1, "title": "Result Two", "url": "http://www.resulttwo.com" }, { "count": 2, "title": "Result Three", "url": "http://www.resultone.com" }, ]

Ahora, para la segunda parte, necesito leer este archivo JSON, visitar cada URL enumerada, extraer algunos datos de la página y agregarlos al objeto JSON actual en el archivo original.

Una vez que se crea el archivo JSON, puedo ejecutar lo siguiente:

 let json_url_list = require('./' + outputFile); // Loop over the URLS for(i=0; i<json_url_list.length; ++i) { let url = json_url_list[i].url; // Run a function here to visit the URL and extract data getNewData(url) }

Junto con una función como esta:

 // Create new function to visit each of the URLs captured. const getNewData = async(url) => { try { const response = await axios.get(url) const $ = cheerio.load(response.data); // Get the data here (using page title for example) const title = $('title').text(); // TODO: Add the new data above to the original JSON object in the file we're reading from return false; } catch (error) { console.error(error) } }

Pero, aquí es donde me quedo sin ideas sobre cómo hacer que esto funcione... ¿Alguien podría indicarme la dirección correcta?

¡Gracias!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Prueba esto.

https://stackblitz.com/edit/js-tmmnqj?file=index.js

 // Create new function to visit each of the URLs captured. const getNewData = async (url) => { try { const response = await axios.get(url); const $ = cheerio.load(response.data); // Get the data here (using page title for example) const title = $('title').text(); console.log(title); // TODO: Add the new data above to the original JSON object in the file we're reading from return false; } catch (error) { console.error(error); } }; (async () => { // Loop over the URLS for (const jsonData of json_url_list) { let url = jsonData.url; // Run a function here to visit the URL and extract data const data = await getNewData(url); } })();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda