nodeJs爬取网络图片

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下载完成")
            })
        }
    })
})
posted @ 2021-05-30 12:32  星时代曹波涛  阅读(166)  评论(0编辑  收藏  举报