javascriptweb-scrapingaxioscheerio

How to select an image url using cheerio and axios


const express = require("express");
const cheerio = require("cheerio");
const axios = require("axios");
const cors = require("cors");
const app = express();

async function getSearchResults(searchFor) {
    const url = `https://www.bol.com/be/nl/s/?searchtext=airpods+pro`;
    const respone = await axios.get(url);
    const $=cheerio.load(respone.data);

    // verwreken van het resultaat in een array
    const ul = $('.product-list');
    ul.find('li .product-item__image .h-o-hidden a .skeleton-image').each((i, element) => {
        const $element = $(element);
        const a = $element.find('img').attr('src');
        console.log(a);   
    });
}

I also give the HTML of the website I'm trying to scrape

So far it recognizes that there are indeed ~20 pictures but it gives an undefined value...


Solution

  • Your problem is that the first img inside this div does not have a src attribute.

    Here is the working code:

    const ul = $('.product-list');
    ul.find('li .product-item__image .h-o-hidden a .skeleton-image')
      .each((i, element) => {
        const $element = $(element);
        const a = $element.find('img').attr('data-src');
        console.log(a);   
     });
    
    

    proof of work screenshot