Browse Source

拆分代码

main
YuJian920 3 years ago
parent
commit
314a3cbe7d
  1. 1
      .gitignore
  2. 39
      utils/saveImages.js
  3. 32
      yitudao.js

1
.gitignore vendored

@ -40,6 +40,7 @@ build/Release @@ -40,6 +40,7 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
Result/
# TypeScript v1 declaration files
typings/

39
utils/saveImages.js

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
const axios = require("axios");
const fs = require("fs");
/**
* 图片下载
* @param {*} imgObject 图片 URL
*/
async function saveImages(imgObject) {
try {
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("目录创建失败");
});
} else {
fs.writeFileSync(`./Result/${imTitle}/${filename}`, data, "binary");
}
});
}
}
} catch (error) {
console.log(`saveImages: 下载图片时出现错误!`);
console.log(error);
}
}
module.exports = saveImages;

32
yitudao.js

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
const saveImages = require("./utils/saveImages");
const request = axios.create({
methods: "GET",
@ -36,7 +36,7 @@ urlArray = [ @@ -36,7 +36,7 @@ urlArray = [
"https://www.yitudao.com/meinv/rentiyishu/",
];
let CURRY_PAGENUMBER = 1; // 爬取起始页码
let CURRY_PAGENUMBER = 11; // 爬取起始页码
let MAX_PAGENUMBER = 573; // 爬取最大页码
// 爬取队列
@ -101,32 +101,4 @@ const loadImages = async (url) => { @@ -101,32 +101,4 @@ const loadImages = async (url) => {
}
};
// 图片下载
const saveImages = async (imgObject) => {
try {
for (const imTitle in imgObject) {
imgObject[imTitle].forEach(async (eachItem, index) => {
console.log("正在下载 ===>", imTitle, index);
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("目录创建失败");
});
} else {
fs.writeFileSync(`./Result/${imTitle}/${filename}`, data, "binary");
}
});
});
}
} catch (error) {
console.log(`saveImages: 下载图片时出现错误!`);
console.log(error);
}
};
spiderQueue(urlArray[0]);

Loading…
Cancel
Save