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

107
Vistas
Cheerio is extracting empty text and undefined href

I am trying to scrape the sports news from The Guardian UK using Cheerio. But, all I am getting is an empty string from the 'a' tag and 'undefined' for 'href' attribute.

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()


I have tried adding a fake user-agent to the axios header, did not work.

However, it printed the texts when I ran console.log($('a').text()) outside the loop to check whether it was actually retrieving anything whatsoever. But in that each block it is returning all empty.

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