Home > Enterprise >  Can't fix an error caused by cheerio while using axios
Can't fix an error caused by cheerio while using axios

Time:09-18

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

CodePudding user response:

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();
  • Related