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

173
Views
jQuery/Cheerio: ¿Cómo obtener recursivamente ciertos elementos por nombre/etiqueta?

Estoy tratando de hacer un bot que raspe los enlaces de un sitio web. Me encuentro con un error extraño que no puedo resolver. Mi conjetura es que la segunda declaración if no se verifica y también mi falta de familiaridad con jQuery no está ayudando.

Error:

element.each no es una función

 const $ = load(html); const html = $("#id"); const temp = []; function recursive(element) { if (element.name === "a") { temp.push(element); } if (!element || element.children().length > 0 === false) { return "DID NOT FIND IT OR NO CHILDREN FOUND"; } return element.each((_, item) => recursive(item)); } recursive(html); return temp;
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Intenté crear un fragmento simple que demuestre lo que parece lograr con JQuery.

En primer lugar, su verificación si la etiqueta de un elemento no parece funcionar correctamente. Tuve que usar .prop('tagName') para obtener la etiqueta del elemento. Y se devuelve en todas las letras mayúsculas.

Su segunda declaración IF debería funcionar bien, pero el .each() funcionó como se esperaba. Desea iterar a través de todos los elementos secundarios e iniciar la función recursiva. Y la forma en que proporcionó el elemento secundario no terminó funcionando. El .each() quiere una función de devolución de llamada que proporcione dos parámetros, ya que tiene usos correctos. pero el elemento es un nodo HTML normal y tuvo que seleccionarlo con JQuery Constant $ like $(item) . Esto le da el elemento JQuery deseado con el que puede trabajar.

 const html = $("#test"); const temp = []; function recursive(element) { if (element.prop("tagName") === "A") { temp.push(element); } if (!element || element.children().length > 0 === false) { return "DID NOT FIND IT OR NO CHILDREN FOUND"; } return element.children().each((i, item) => recursive($(item))); } recursive(html); console.log(temp)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="test"> <div class="headings"> <h1>Heading</h1> <a href="./main.html">Main Page</a> </div> <div class="test-cls"> <button>Hello</button> <a href="./test.html">Test Page</a> </div> </div>

about 4 years ago · Santiago Trujillo 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!