//引入模块
var http = require(“http”);
var https = require(“https”);
var fs = require(“fs”);
var arr = [];
var url2 = “https://www.ivsky.com/tupian/renwutupian/”
var htmldata = “”;
//获取html文档
https.get(url2, function(res) {
res.on(“data”, function(chunk) {
htmldata += chunk;
});
res.on(“end”, function() {
//调用正则表达函数
reg(htmldata);
})
});
//正则表达式 提取链接
function reg(htmldata) {
var result;
var reg = /img src="(.*?)"/mg;
while ((result = reg.exec(htmldata)) != null) {
arr.push(result[1]);
}
dowload(arr);//调用函数
}
//下载图片
function dowload(arr) {
for (let i = 0; i < arr.length; i++) {
(function(i) {
setTimeout(function() {
https.get(“https:” + arr[i], function(res) {
var imgData = “”;
res.setEncoding(“binary”);//一定要设置response的编码为binary否则会下载下来的图片打不开
res.on(“data”, function(chunk) {
imgData += chunk;
});
res.on(“end”, function() {
fs.writeFileSync("./img/" + i + “.jpg”, imgData, “binary”)//同步写入文件
});
})
}, 1000 * i);
})(i);
}
}
05-21
551
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)