• Node.js meitulu图片批量下载爬虫1.01版


    在 http://www.cnblogs.com/xiandedanteng/p/7614051.html 一文我曾经书写过一个图片下载爬虫,但原有程序不是为下载图片而设计故有些绕,于是稍微改写了一下,可读性应该稍好些。功能上和原程序差不多,只是输出目录不是固定在test目录了。代码如下:

    //================================================
    // https://www.meitulu.com图片批量下载Node.js爬虫1.01
    // 2017年11月5日
    //================================================
    
    
    // 内置http模块,提供了http服务器和客户端功能
    var http=require("http");
    
    // cheerio模块,提供了类似jQuery的功能
    var cheerio = require("cheerio");
    
    // 内置文件处理模块
    var fs=require('fs');
    
    // 请求参数JSON
    var options;
    
    // request请求
    var req;
    
    //--------------------------------------
    // 程序入口
    //--------------------------------------
    function start(){
        var folder="38";
        fs.mkdir('./'+folder,function(err){
            if(err){
                console.log("创建目录"+folder+"失败");
            }
        });
    
        var startIndex=1;
        var endIndex=104;
    
        for(var i=startIndex;i<=endIndex;i++){
            downloadPic(folder,i);
        }
    }
    
    //--------------------------------------
    // 下载图片
    // folder:图片所在url的目录
    // pinctureIndex:图片序号
    //--------------------------------------
    function downloadPic(folder,pinctureIndex){
        console.log("开始下载"+pinctureIndex);
    
        // 初始化options
        options={
            hostname:'mtl.ttsqgs.com',// 这里别加http://,否则会出现ENOTFOUND错误
                port:80,
                path:'/images/img/'+folder+'/'+pinctureIndex+'.jpg',// 子路径
              method:'GET',
        };
    
        req=http.request(options,function(resp){
            var imgData = "";
            resp.setEncoding("binary"); 
    
            resp.on('data',function(chunk){
                imgData+=chunk;            
            });
    
            resp.on('end',function(){
                var fileName="./"+folder+"/"+pinctureIndex+".jpg";
                fs.writeFile(fileName, imgData, "binary", function(err){
                    if(err){
                        console.log("文件"+fileName+"下载失败.");
                    }
                    console.log(fileName+"下载成功");
                });    
            });
        });
    
        // 超时处理
        req.setTimeout(5000,function(){
            req.abort();
        });
    
        // 出错处理
        req.on('error',function(err){
            if(err.code=="ECONNRESET"){
                console.log('socket端口连接超时。');
            }else{
                console.log('请求发生错误,err.code:'+err.code);
            }
        });
    
        // 请求结束
        req.end();
    }
    
    
    // 调用start函数,程序开始
    start();
  • 相关阅读:
    go语言第一问:在其他地方执行编译go语言程序,结果会在哪个地方产生?
    ip地址获取无效,自己修改ip地址
    linux和windows双向互通的压缩包格式zip
    在notepad++中tab和空格的区别
    Django ----- app 和 ORM的操作和介绍
    Mysql --- 索引
    Mysql --创建用户和授权,备份
    Mysql --数据的增删改
    Mysql -- 外键的变种 三种关系
    Mysql -- 完整性约束
  • 原文地址:https://www.cnblogs.com/heyang78/p/7788877.html
Copyright © 2020-2023  润新知