我正在使用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 中获取这些图像链接?
答案 0 :(得分:0)
替换
"Image" :loadedhtml(imglink[i])
与
"Image" :loadedhtml(imglink[i]).attr('data-src')