async downloadImage(data, index) {
if (data.res) {
var $ = cheerio.load(data.res);
if ($(".main-image").find("img")[0]) {
let imgSrc = $(".main-image").find("img")[0].attribs.src;
let headers = {
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
Host: "i.meizitu.net",
Pragma: "no-cache",
"Proxy-Connection": "keep-alive",
Referer: data.url,
"Upgrade-Insecure-Requests": 1,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.19 Safari/537.36"
};
await rp({
url: imgSrc,
resolveWithFullResponse: true,
headers
}).pipe(fs.createWriteStream(`${downloadPath}/${index}.jpg`));
console.log(`${downloadPath}/${index}.jpg下载成功`);
} else {
console.log(`${downloadPath}/${index}.jpg加载失败`);
}
}
}