简体   繁体   中英

How to get these Img src tags using scraping

I am doing web scraping using cheerio, below is my code

let request = require("request");
let fs = require("fs");
let cheerio = require("cheerio");
const { load } = require("cheerio");
let data = [];
let loadedhtml;
request("https://www.dineout.co.in/delhi-restaurants/west-delhi/dwarka", function (err, res, 
body) {

if (!err) {
    loadedhtml = cheerio.load(body);
    let RN = loadedhtml(".restnt-name.ellipsis");
    let Detail = loadedhtml(".double-line-ellipsis");
    let Loc=loadedhtml(".restnt-loc.ellipsis");
    let imglink=loadedhtml('.no-img');
    
    // console.log(loadedhtml(imglink));
    for (let i = 1; i < RN.length; i++) {
        let obj = {
            "Restaurant Name": loadedhtml(RN[i]).text().trim(),
            "Details": loadedhtml(Detail[i]).text().trim(),
            "Location" : loadedhtml(Loc[i]).text().trim().trim(),
            "Image" :loadedhtml(imglink[i]),
            "id":i-1
        }
        data.push(obj);
    }
    fs.writeFileSync("data.js", JSON.stringify(data));
}
});

I am trying to get the image src links but i am getting errors one way or the other. Can anyone suggest how to get those image links inside data obj?

Replace

"Image" :loadedhtml(imglink[i])

with

"Image" :loadedhtml(imglink[i]).attr('data-src')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM