Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda