Node进程为什么不会自动退出呢?
const https = require("https");
const http = require("http");
const fs = require("fs");
const browser = {
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
};
const httpP = (...props) =>
new Promise((resolve, reject) => {
const req = http.request(...props, function (req) {
const { statusCode } = req;
// console.log("httpP", statusCode);
if (statusCode >= 200 && statusCode < 300) {
resolve(req);
} else {
reject(req);
}
});
req.end();
});
const httpsP = (...props) =>
new Promise((resolve, reject) => {
const req = https.request(...props, function (req) {
const { statusCode } = req;
// console.log("httpsP", statusCode);
if (statusCode >= 200 && statusCode < 300) {
resolve(req);
} else {
reject(req);
}
});
req.on("error", function (err) {
console.log("请求报错", err);
reject(req);
});
req.end();
});
const getData = (req) =>
new Promise((resolve, reject) => {
if (req.on) {
let data = "";
req.on("data", function (chunk) {
data += chunk;
});
req.on("end", function () {
resolve(data);
});
} else {
reject(req);
}
});
function getImgUrlFormDocument(document, imgProps = "src") {
const img = document.match(/(<img(.|\n)*?)>/g);
if (imgProps === "img") {
return img;
}
const imgUrlProps = new RegExp(`(${imgProps}=['"].*?)['"]`, "g");
const imgUrl = img
.map((item) => item.match(imgUrlProps) && item.match(imgUrlProps)[0])
.filter((item) => item);
if (!imgUrl.length) {
throw new Error(`未匹配img属性${imgProps}`);
}
return imgUrl;
}
const saveImg = (imgData, saveLoaclPath, fileName) =>
new Promise((resolve, reject) => {
fs.mkdir(saveLoaclPath, { recursive: true }, function () {
fs.writeFile(saveLoaclPath + "/" + fileName, imgData, "binary", function (
err
) {
if (err) {
reject(err);
return console.log("图片保存失败", err);
}
resolve(`${saveLoaclPath.slice(2)}/${fileName} save success !!!`);
// console.log(`${saveLoaclPath.slice(2)}/${fileName} save success !!!`);
});
});
});
const query = (page) =>
httpsP(`https://www.doutula.com/photo/list/?page=${page}`, browser)
// 获取html
.then((req) => getData(req))
// 获取页面图片的路径
.then((data) =>
getImgUrlFormDocument(data, "data-original").map((item) =>
item.slice(15, -1)
)
)
// 请求图片
.then((img) => Promise.all(img.map((item) => httpP(item, browser))))
// 保存图片数据
.then((dataAll) =>
dataAll.map((item) => {
item.setEncoding("binary");
const path = item.connection._httpMessage.path;
return { data: getData(item), name: path.split("/").pop() };
})
)
.then(
(imgData) =>
Promise.all(
imgData.map((item) =>
item.data.then((imgData) =>
saveImg(imgData, `./Biaoqing/第${page}页`, item.name)
)
)
)
// imgData.forEach((item, index) => {
// item.data.then((imgData) => {
// saveImg(imgData, `./Biaoqing/第${page}页`, item.name);
// });
// })
)
.then((result) => console.log(`./Biaoqing/第${page}页`, result))
.catch((err) => console.error("ERROR: ", err));
Promise.all(
Array.from({ length: 5 }).map((item, index) => query(index + 1))
).then(() => console.log("全部请求完成!!!"));
运行程序的时候没有报错,图片也保存了,但是程序运行完不会退出,到底是什么原因?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我试了一下5页表情都保存成功了
程序不会退出是指开发的时候关闭不了node?
是的话可以试试这个nodemon
https://www.jianshu.com/p/3b3...