当前位置:网站首页>Nodejs crawler

Nodejs crawler

2022-07-06 15:59:00 Star age Cao Botao

nodejs You can also play with reptiles

Need to pass through npm Down in two modules .

npm init -y
npm install -S axios cheerio

establish index.js file

const cheerio = require("cheerio")
const axios = require("axios")
const fs = require("fs")
if (!fs.existsSync("download")) {
    
    fs.mkdirSync("download", 777)
}
console.log(" Start crawling ")
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(" Start the download " + url)
            axios.get(url, {
     responseType: 'stream' }).then(resp => {
    
                let writer = fs.createWriteStream("./download/" + index + ".png");
                resp.data.pipe(writer);
                console.log(index+".png Download complete ")
            })
        }
    })
})

cmd Input

node index.js

You can see that the file has been downloaded

原网站

版权声明
本文为[Star age Cao Botao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919407612.html