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

1.4K
Views
¿Cómo puedo obtener la imagen src, el título y la descripción de este html usando cheerio?

Estoy tratando de extraer contenido del sitio web usando nodejs con cheerio. Quiero extraer el siguiente contenido:

  1. Texto "Este es mi texto de título de muestra".
  2. Texto "Aquí estará el contenido de mi descripción".
  3. Origen de la imagen.

Aquí está el html:

 <body> <div class="detail_loop"> <img class="imfast" data-original="http://www.example.com/wp-content/uploads/2017/03/imageurl-250x150.jpg" title="" align="left" width="250" height="150" src="http://www.example.com/wp-content/uploads/2017/03/imageurl-250x150.jpg" style="display: block;"> <h2> <a href="http://www.example.com/2017/04/576487/" rel="bookmark">This is my titile text</a> </h2> Here will be my description content. <div class="clear"></div> <div class="send_loop" style="display: none;"> <a href="http://www.example.com/2017/04/576487//#respond" target="_blank"> <div class="send_com"> <div class="send_bubb"> <div class="count"> 0 </div> </div> </div> </a> <a href="https://www.facebook.com/sendr.php?u=http://www.example.com/2017/04/576487/" target="_blank"> <div class="send_fb"> <div class="send_bubb"> <div class="count"> send </div> </div> </div> </a> <a href="https://twitter.com/send?url=http://www.example.com/2017/04/576487/&amp;text=this is sample title;hashtags=example" target="_blank"> <div class="send_tt"> <div class="send_bubb"> <div class="count"> Tweet </div> </div> </div> </a> <div class="clear"></div> </div> <div class="clear"></div> <div class="detail_loop_dvd"></div> <div class="clear"></div> </div> </body>
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

¿Algo así a lo que apuntabas? Por supuesto, podría simplemente pasar los datos a la: cheerio.load('<html><body>…</html>')

Código de ejemplo

Nota: .text() devolverá todos los hijos (otros <div>, etc.), por lo tanto, el filtro que devuelve verdadero solo en los nodos de texto. –[ #20832910 ]

 const cheerio = require('cheerio'); const fs = require('fs'); /** * Given data saved in file 'index.html' in current path */ fs.readFile('index.html', {encoding: 'utf-8'}, (err, data) => { if (err) { console.log(err); return; } const $ = cheerio.load(data); /** * Print what you desire */ console.log($('h2 a').text()); // Title text console.log($('div.detail_loop').contents().filter( function() { return this.type === 'text'; }).text()); // Description content (without child nodes--only text) console.log($('img').attr('src')); // Image source });
over 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!