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

239
Vistas
How will I get the content ie the title of a tag while web scrapping with puppeteer?

I am following this tutorial. https://www.digitalocean.com/community/tutorials/how-to-scrape-a-website-using-node-js-and-puppeteer

I am trying to learn how to use puppeteer.

I am trying to get whats written in the anchor tag .

So my code in pageScraper file is this

const scraperObject = {
    url: 'http://books.toscrape.com',
    async scraper(browser){
        let page = await browser.newPage();
        console.log(`Navigating to ${this.url}...`);
        // Navigate to the selected page
        await page.goto(this.url);
        // Wait for the required DOM to be rendered
        await page.waitForSelector('.page_inner');      

        let mytext=await page.$$eval('.nav-list li > a',text => text.textContent)
        
        console.log(mytext);
    }
}//         dataObj['bookTitle'] = await newPage.$eval('.product_main > h1', text => text.textContent);
module.exports = scraperObject;

However I am getting an undefined while printing.

I am trying to get this. Selectors

book text

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

0

First of all, http://books.toscrape.com doesn't have any functional JS, so there's no need for Puppeteer. Better to just use a lightweight static request library like fetch or axios and an HTML parser like Cheerio.

That said, for pedagogical purposes,

await page.$$eval('.nav-list li > a',text => text.textContent)

appears to be the problem. $$eval means "select all and run a callback on all selected elements in the browser context", similar to document.querySelectorAll. So the variable you call text is an array of elements, not a single element, and the array property .textContent is undefined.

If you want to select a single element, use $eval. Please call the variable element or el, not text.

On the other hand, if you want to select multiple elements, treat it like an array in the callback.

Getting one element's text:

await page.$eval('.nav-list li > a', el => el.textContent);

Getting multiple elements' text:

await page.$$eval('.nav-list li > a', els => els.map(el => el.textContent));
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