上次我们已经成功的爬取了网站上的图片,现在我们把爬取的图片存储到本地
http.get(url, function (res) { var imageData =‘‘; res.setEncoding("binary"); res.on(‘data‘,function(data){ //图片加载到内存变量 imageData += data; }).on(‘end‘,function(){ //图片加载完 var $=cheerio.load(imageData); $Imgs = $(‘img‘), $Imgs.each((iten,i)=>{ console.log($(i).attr(‘src‘)+"------"); var urlimg=$(i).attr(‘src‘); var filename= path.basename(urlimg);; downloadImg(urlimg,filename) }) }); }); function downloadImg(url, filename) { request(url).on(‘error‘,function(){ console.log(‘done no‘); }).pipe(fs.createWriteStream(‘images/‘ + filename));//保存图片到文件夹 }
原文地址:https://www.cnblogs.com/aSnow/p/8438945.html
时间: 2024-10-31 03:40:13