|
|
|
@ -5,35 +5,42 @@ const fs = require("fs");
@@ -5,35 +5,42 @@ const fs = require("fs");
|
|
|
|
|
* 图片下载 |
|
|
|
|
* @param {*} imgObject 图片 URL |
|
|
|
|
*/ |
|
|
|
|
async function saveImages(imgObject) { |
|
|
|
|
try { |
|
|
|
|
function saveImages(imgObject) { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
const forItem = Object.keys(imgObject); |
|
|
|
|
for (let forItemIndex = 0; forItemIndex < forItem.length; forItemIndex++) { |
|
|
|
|
const imTitle = forItem[forItemIndex]; // 文件名
|
|
|
|
|
for (let urlIndex = 0; urlIndex < imgObject[imTitle].length; urlIndex++) { |
|
|
|
|
const eachItem = imgObject[forItem][urlIndex]; // url
|
|
|
|
|
console.log("正在下载 ===>", imTitle, urlIndex + 1, eachItem); |
|
|
|
|
|
|
|
|
|
const filename = eachItem.split("/").pop(); |
|
|
|
|
const { data } = await axios({ |
|
|
|
|
url: eachItem, |
|
|
|
|
responseType: "arraybuffer", |
|
|
|
|
}); |
|
|
|
|
fs.access(`./Result/${imTitle}`, (accessErr) => { |
|
|
|
|
if (accessErr) { |
|
|
|
|
fs.mkdir(`./Result/${imTitle}`, (mkdirErr) => { |
|
|
|
|
if (mkdirErr) console.log("目录创建失败"); |
|
|
|
|
fs.access(`./Result/${imTitle}`, async (accessErr) => { |
|
|
|
|
if (accessErr) fs.mkdirSync(`./Result/${imTitle}`); |
|
|
|
|
else { |
|
|
|
|
const alreadyPath = fs.readdirSync(`./Result/${imTitle}`); |
|
|
|
|
// 已有文件跳过
|
|
|
|
|
if (alreadyPath.length === imgObject[imTitle].length) { |
|
|
|
|
console.log(`${imTitle} 已存在 跳过抓取`); |
|
|
|
|
resolve() |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
for (let urlIndex = 0; urlIndex < imgObject[imTitle].length; urlIndex++) { |
|
|
|
|
const eachItem = imgObject[forItem][urlIndex]; // url
|
|
|
|
|
if (!eachItem) continue; |
|
|
|
|
console.log("正在下载 ===>", imTitle, urlIndex + 1, eachItem); |
|
|
|
|
try { |
|
|
|
|
const filename = eachItem.split("/").pop(); |
|
|
|
|
const { data } = await axios({ |
|
|
|
|
url: eachItem, |
|
|
|
|
responseType: "arraybuffer", |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
fs.writeFileSync(`./Result/${imTitle}/${filename}`, data, "binary"); |
|
|
|
|
} catch (error) { |
|
|
|
|
console.log(`saveImages: 下载图片时出现错误!`); |
|
|
|
|
console.log(error); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
resolve(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} catch (error) { |
|
|
|
|
console.log(`saveImages: 下载图片时出现错误!`); |
|
|
|
|
console.log(error); |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
module.exports = saveImages; |
|
|
|
|