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

215
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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