爬虫(文字+图片)

图片抓取案例

var http=require("http")
var fs=require("fs")
var cheerio=require("cheerio");
var sd=require("silly-datetime")
var path=require("path")


http.get("http://www.mm131.com",function(res){
     

    var result="";
    res.on("data",function(chunk){
        
        result+=chunk
    })

    res.on("end",function(){

        // console.log(result.toString())
        var $=cheerio.load(result);

        // console.log($("dd a"))

        $("dd+dd a").each(function(index,value){
            // console.log($(value).attr("href"))

            http.get($(value).attr("href"),(res1)=>{
                var result1="";
                res1.on("data",(chunk)=>{
                    result1+=chunk
                })
                res1.on("end",()=>{
                    // console.log(result1.toString())

                    var $$=cheerio.load(result1);

                    $$(".content-pic img").each(function(index,value){
                        // console.log($(value).attr("src"));
                        http.get($(value).attr("src"),function(res3){
                            var ttt=sd.format(new Date(),"YYYYMMDDHHmmss");
                            var ran=Math.floor(Math.random()*10000+8999);
                            var extname=path.extname($(value).attr("src"));
                            var  writeStream=fs.createWriteStream(path.join(__dirname,"./upload/",ttt+ran+extname));
                            res3.pipe(writeStream).on("close",function(){
                                console.log("下载完毕")
                            })
                        })
                    })
                })
            })
        })

    })
})

文字抓取案例

var http=require("http")
var fs=require("fs")
var cheerio=require("cheerio");

http.get("http://unclealan.cn/index.php",function(res){

         var result="";
         res.on("data",function(chunk){     
         result+=chunk
       })

         res.on("end",function(){
             
             var $=cheerio.load(result);
             $(".post-permalink a").each(function(index,value){

                http.get($(value).attr("href"),function(res1){
                    var result1="";
                    res1.on("data",(chunk)=>{
                        result1+=chunk
                    })
                    res1.on("end",function(){
                        var $$=cheerio.load(result1);
                        $$(".post-content").each(function(index,value){
                            
                            console.log($(value).text());

                            fs.appendFile('message1.txt',$(value).text(), (err) => {
                            
                                });
                        })
                    })
                })
             })
        })
    })


你可能感兴趣的:(爬虫(文字+图片))