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

201
Vistas
How to get a certain element from a body?

I use node-fetch , and I get the body of the site this way:

import fetch from 'node-fetch';

(async () => {
    const response = await fetch('link');
    const body = await response.text().

    console.log(body);
})()

The console displays the full body of the entire page. But I want to get a specific element with a certain class. How do I change the code to do this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use cheerio.js. It is an implementation of jQuery for node.

The below code selects an h2 and changes its text to Hello World.

const cheerio = require('cheerio');
const $ = cheerio.load(body);

$('h2').text('Hello World!');
about 4 years ago · Juan Pablo Isaza Denunciar

0

Well, you've already achieved obtaining a string that contains the entire html of the page.

Now you can write code that extracts the part you want from that string. You said you're wanting to extract an element that has a particular class.

As moonwave99 commented, you could use cheerio, or some other html parser to extract the element with the class name you're targeting.

Or, if you want to avoid using an outside package, you could just write a regular expression to match the HTML element you want from that html string:

let wholeHTML =
`<html>
<p>Paragraph 1</p>
<p class="classIwant">Paragraph 2</p>
<p>Paragraph 3</p>
</html>`;

let rx = /<p class="classIwant".*\/p>/gm;
let matches = wholeHTML.match(rx);
console.log(matches[0]);

about 4 years ago · Juan Pablo Isaza Denunciar
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