const cheerio = require("cheerio")
const axios = require("axios")
const fs = require("fs")
if (!fs.existsSync("download")) {
fs.mkdirSync("download", 777)
}
console.log("开始爬取")
axios.get("http://www.scsoul.top/list/1/20").then(r => {
const $ = cheerio.load(r.data)
$("img").each((index, item) => {
if (index != 0) {
var url = $(item).attr("src");
console.log("开始下载" + url)
axios.get(url, { responseType: ‘stream‘ }).then(resp => {
let writer = fs.createWriteStream("./download/" + index + ".png");
resp.data.pipe(writer);
console.log(index+".png下载完成")
})
}
})
})
nodeJs爬取网络图片