var script = document.createElement('script'); script.src = "https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js";
document.head.appendChild(script);
var arr_record = [];
// 图片下载
var download_img = function (img_src, name) {
var image = new Image();
image.setAttribute("crossOrigin", "anonymous");
image.onload = function () {
var canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext("2d");
context.drawImage(image, 0, 0, image.width, image.height);
var url = canvas.toDataURL("image/png");
var a = document.createElement("a");
var event = new MouseEvent("click");
a.download = name || "photo";
a.href = url;
a.dispatchEvent(event);
};
image.src = img_src;
};
// 评价图片-详情
var tianmao_comment_pic_detail = function () {
console.log('评价图片-开始下载');
var child = $("#reviews div.J_KgRate_MainReviews").children()[1];
$(child).find("li.photo-item img").each(function () {
var url = "https:" + $(this).attr("src").replace("_40x40.jpg", "");
var index = arr_record.length;
var name = "img" + index + ".png";
arr_record.push({
name: name,
url: url
});
});
console.log('评价图片-下载完成');
};
// 评价图片-翻页
var tianmao_comment_pic_jump = function (para) {
// 下载所有图片
var download_all_pic = function () {
console.log(arr_record);
for (var i = 0; i < arr_record.length; i++) {
var item = arr_record[i];
download_img(item.url, item.name);
}
arr_record = [];
};
var num = 1;
var id = setInterval(function () {
tianmao_comment_pic_detail();
// 指定抓取页数
if ((para.num > 0) && (num >= para.num)) {
console.log("数据抓取完成,开始下载!");
window.clearInterval(id)
download_all_pic();
return;
}
// 一直抓取完
var lastDom = $("#reviews div.J_KgRate_MainReviews li.pg-next:last");
if (lastDom.hasClass("pg-disabled")) {
console.log('数据抓取完成,开始下载!');
window.clearInterval(id)
download_all_pic();
return;
}
lastDom.click();// 下一页
num++;
}, 2000);
};
// 这里指定下载的页数
tianmao_comment_pic_jump({ num: 2 });
标签:function,arr,image,pic,图片下载,record,淘宝,var,电商
From: https://www.cnblogs.com/guxingy/p/16791184.html