Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Al ejecutar un script, siempre obtengo "indefinido" como resultado

He estado tratando de raspar el enlace de la página siguiente de forma iterativa desde una página web usando axios y cheerio . Cuando ejecuto el script, como resultado siempre me undefined . ¿Cómo puedo obtener el enlace de la página siguiente hasta que no haya más?

Este es mi intento actual:

 const axios = require("axios"); const cheerio = require("cheerio"); const base = "https://stackoverflow.com"; const url = "https://stackoverflow.com/questions/tagged/web-scraping"; async function main(){ const data = await axios.get(url,{ headers: { "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36" } }); const $ = cheerio.load(data); let nextPage = $('a[rel="next"]').eq(0).attr("href"); console.log(nextPage); while(nextPage){ url = base + nextPage; const data = await axios.get(url,{ headers: { "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36" } }); const $ = cheerio.load(data); nextPage = $('a[rel="next"]').eq(0).attr("href"); console.log(nextPage); } } main();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La promesa resuelta de axios.get es un objeto de respuesta que contiene el cuerpo de respuesta html real en <axiosResponse>.data . Entonces, si lo cambia a lo siguiente, debería funcionar:

 ... const response = await axios.get(url,{ headers: { "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36" } }); const $ = cheerio.load(response.data); ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!