如何使用抓取来获取这些 Img src 标签

时间:2021-06-10 17:42:12

标签: javascript node.js web cheerio

我正在使用cheerio进行网络抓取,下面是我的代码

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));
}
});

我正在尝试获取图像 src 链接,但我以一种或另一种方式收到错误。谁能建议如何在 data obj 中获取这些图像链接?

1 个答案:

答案 0 :(得分:0)

替换

"Image" :loadedhtml(imglink[i])

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