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

233
Views
¿Cómo obtendré el contenido, es decir, el título de una etiqueta durante el desguace web con titiritero?

Estoy siguiendo este tutorial. https://www.digitalocean.com/community/tutorials/how-to-scrape-a-website-using-node-js-and-puppeteer

Estoy tratando de aprender a usar titiritero.

Estoy tratando de obtener lo que está escrito en la etiqueta de anclaje.

Entonces mi código en el archivo pageScraper es este

 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;

Sin embargo, obtengo un mensaje indefinido mientras imprimo.

Estoy tratando de conseguir esto. Selectores

texto del libro

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En primer lugar, http://books.toscrape.com no tiene ningún JS funcional, por lo que no es necesario Titiritero. Es mejor usar una biblioteca de solicitud estática liviana como fetch o axios y un analizador HTML como Cheerio.

Dicho esto, a efectos pedagógicos,

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

parece ser el problema. $$eval significa "seleccionar todo y ejecutar una devolución de llamada en todos los elementos seleccionados en el contexto del navegador", similar a document.querySelectorAll . Entonces, la variable a la que llama text es una matriz de elementos, no un solo elemento, y la propiedad de la matriz .textContent no está definida.

Si desea seleccionar un solo elemento, use $eval . Llame al element variable o el , no al text .

Por otro lado, si desea seleccionar varios elementos, trátelos como una matriz en la devolución de llamada.

Obtener el texto de un elemento:

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

Obtener el texto de varios elementos:

 await page.$$eval('.nav-list li > a', els => els.map(el => el.textContent));
about 4 years ago · Juan Pablo Isaza 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!