Q: How to get actual URL in img src attribute instead of Base64 encoded image using Cheerio? I'm using a form to POST a URL and process it to get data from a website for scraping purposes. Everything works fine except the src of the image returns a base64 image file instead of the actual URL.
https://www.amazon.in/Arden-Grange-Mini-Adult-Food/dp/B01EVLSZN2/
I tried to get the same data via a data-old-hires attribute but not all pages have that on the website. So I can't rely on that.
try {
    const html = await request.get(url)
    const $ = await cheerio.load(html)

    const imageURL = $('#imgTagWrapperId').find('img').attr('src')

    res.render('results', {
        imageURL
    })
} catch (err) {
    console.log(`APP ERROR: ${err.message}`)
    res.render('index', {
        err
    })
}

So how do I get the actual data inside the src attribute and not the base64 encoded image?

A: I think the URL of image is hiding somewhere in the data-a-dynamic-image attribute value. So you can parse the value, which is JSON format.
const cheerio = require ('cheerio')
const express = require ('express')
const request = require ('request-promise')
const app = express()
const port = 3000
const url = 'https://www.amazon.in/Arden-Grange-Mini-Adult-Food/dp/B01EVLSZN2/'

app.get('/', async (req, res) => {

    try {
        const html = await request(url)
        const $ = await cheerio.load(html)

        const imageURLJSON = JSON.parse($('#imgTagWrapperId img').attr('data-a-dynamic-image'))

        res.send (imageURLJSON)
    } catch (err) {
        console.log(`APP ERROR: ${err.message}`)
        res.render('index', {
            err
        })
    }

})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

