Estoy tratando de obtener las noticias deportivas de The Guardian UK usando Cheerio. Pero, todo lo que obtengo es una cadena vacía de la etiqueta 'a' y 'indefinido' para el atributo 'href'.
const axios = require("axios") const cheerio = require("cheerio") const url = "https://www.theguardian.com/uk/sport" const news = [] function getSportsNews() { axios.get(url).then((response) => { const html = response.data const $ = cheerio.load(html) $('a', html).each(() => { const title = $(this).text() const link = $(this).attr('href') news.push({ title, link }) console.log(news) }) }) } getSportsNews() Intenté agregar un agente de usuario falso al encabezado de axios , no funcionó.
Sin embargo, imprimió los textos cuando ejecuté console.log($('a').text()) fuera del ciclo para verificar si realmente estaba recuperando algo. Pero en eso each bloque está volviendo todo vacío.