Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
raspar datos de varias páginas en un bucle usando javascript

Estoy tratando de extraer datos de un sitio web que tiene varias páginas, el código funciona bien para una sola ejecución, pero cuando trato de ponerlo en un bucle, el sitio web se bloquea y Chrome dice que no tengo memoria. A continuación se muestra el javascript que estoy usando:

 // an XMLHttpRequest var xhr = null; var page = 1; function scrape() { // instantiate XMLHttpRequest object try { xhr = new XMLHttpRequest(); } catch (e) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } // handle old browsers if (xhr == null) { alert("Ajax not supported by your browser!"); return; } // get city var city = document.getElementById("city").value; // get data xhr.onreadystatechange = function() { // only handle loaded requests if (xhr.readyState == 4) { if (xhr.status == 200) { // insert link into DOM some stuff } else alert("Error with Ajax call!"); } } while(xhr.responseText != ""done) { // construct URL var url = "scrape_data.php?city=" + city + "+" + page ; xhr.open("GET", url, true); xhr.send(null); page++; } }

y este es el backend correspondiente:

 // get the q parameter preg_match('/.*(?=\+)/',$_GET["city"],$city); preg_match('/(?<=\+).*/',$_GET["city"],$page); // scrape data from shiksha.com $string = @file_get_contents("http://www.shiksha.com/b-tech/colleges/b-tech-colleges-".urlencode($city)."-{$page}"); if($page === 1) { // counting total number of pages preg_match_all('/class=" linkpagination">/',$string,$result); $GLOBALS["pages"] = sizeof($result[0]); } else $GLOBALS["pages"]--; // passing the string for scraping data and storing in database if($GLOBALS["pages"]=== 0) print("loop");

ahora, ¿cómo hago un bucle a través de todas las páginas y también cómo verifico si todas las páginas han sido analizadas (condición dentro del bucle for)? Gracias de antemano por cualquier ayuda.

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda