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

113
Views
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
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!