PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 13, 2022

[FIXED] How to select an image url using cheerio and axios

 October 13, 2022     axios, cheerio, javascript, web-scraping     No comments   

Issue

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



Answered By - Anthony S
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing