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

206
Views
Can't fix an error caused by cheerio while using axios

I'm trying to fetch titles of different posts from a webpage using axios and cheerio but the script always throws this error Error: cheerio.load() expects a string. How can I fix it?

const axios = require('axios');
const cheerio = require('cheerio');

const startLink = 'https://old.reddit.com/r/programming/';

const getPostTitles = (link) =>{
        const resp = axios.get(link);
        const $ = cheerio.load(resp.data);
        const postTitles = [];

        $('div > p.title > a').each((_idx, el) => {
            const postTitle = $(el).text()
            postTitles.push(postTitle)
        });

        return postTitles;
};

const results = getPostTitles(startLink);
console.log(results);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Axios.get() returns a promise, so you need to either await it , or use .then() to get the result. I've used the async/await syntax below, this will display the post titles:

const axios = require('axios');
const cheerio = require('cheerio');

const startLink = 'https://old.reddit.com/r/programming/';

const getPostTitles = async (link) => {
    const resp = await axios.get(link);
    const $ = cheerio.load(resp.data);
    const postTitles = [];

    $('div > p.title > a').each((_idx, el) => {
        const postTitle = $(el).text()
        postTitles.push(postTitle)
    });

    return postTitles;
};

async function testGetPostTitles() {
    const results = await getPostTitles(startLink);
    console.log(results);
}

testGetPostTitles();
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!