Javascript 提取具有microcontent类属性的图像
我想从html中提取像我一样具有“microcontent”类属性的图像 … 我想提取这个类为class=“microcontent”的图像 我写了这段代码,但它不起作用Javascript 提取具有microcontent类属性的图像,javascript,jquery,node.js,Javascript,Jquery,Node.js,我想从html中提取像我一样具有“microcontent”类属性的图像 … 我想提取这个类为class=“microcontent”的图像 我写了这段代码,但它不起作用 var $ = cheerio.load(html); var title = $('head title').text(); var keywords = $('head meta[name=keywords]').attr('content'); var desc = $('head
var $ = cheerio.load(html);
var title = $('head title').text();
var keywords = $('head meta[name=keywords]').attr('content');
var desc = $('head meta[name=description]').attr('content');
var links = $('a');
var imgArray = [];
$('img.microconent').each(function(){
var temp = $(this).attr("src");
imgArray.push(temp);
var downloadImage = function (temp) {
http.get(temp, function (res) {
var imagedata = ''
res.setEncoding('binary')
res.on('data', function(chunk){
imagedata += chunk;
});
res.on('end', function(){
var imgArr = temp.split("/");
var Name = util.id();
imgName = imgArr[imgArr.length-1];
imgName = imgName.split(".");
imgName = imgName[imgName.length-1];
imgName = Name + "." +imgName;
fs.writeFile(__dirname + '/img/' + Name + ".png" , imagedata, 'binary', function(err){
if (err) throw err;
console.log('image saved')
});
});
});
};
downloadImage(temp);
console.log("image is " + imgArray);
});
console.log('Crawling "%s" | %s',title,this.url);
任何帮助都是值得的。不要在“.”之后留出空间 你应该这样写
`$('img .microcontent')` not like `$('img . microcontent`')
$('img.microComponent')
应该是$('img.microComponent')
可以,但它不是提取microcontent类…它不起作用..我的错,针对具有“microcontent”类属性的图像的正确语法是:$('img.microcontent')
(没有空格)好的,但@mguimard图像现在保存在文件夹中,但未加载…您所说的“未加载”是什么意思?使用fs.writeFile在磁盘上写得好吗?您的日志中是否有任何错误?请尝试$(“img[class='microcontent'])