node. js nvshens Image bulk Download crawler 1.00

Source: Internet
Author: User

//======================================================//www.nvshens.com Image Bulk download node. JS Crawler 1.00//This program is similar to the Meitulu crawler, first write a shelf here//November 9, 2017//======================================================//built-in HTTPS modulevarHttps=require ("https");//built-in file processing module for creating directories and picture filesvarFs=require (' FS ');//request parameter JSON. Both HTTP and HTTPS are usedvaroptions;//Request Requestsvarreq;//https://img.onvshen.com:85/gallery/11519/11602/0.jpg '//--------------------------------------//Download Image//folder: The first level directory of the URL where the picture is located//subfolder: The two level directory where the image is in the URL//Pinctureindex: Picture number//--------------------------------------functionDownloadpic (folder,subfolder,pinctureindex) {Console.log ("Picture:" +pinctureindex+ "Download Start"); //Initialize Optionsoptions={hostname:' Img.onvshen.com ', Port:85, Path:'/gallery/' +folder+ '/' +subfolder+ '/' +pinctureindex+ '. jpg ',//Sub-PathMethod: ' GET ',    }; Req=https.request (Options,function(resp) {varImgdata = ""; Resp.setencoding ("Binary"); Resp.on (' Data ',function(chunk) {Imgdata+=Chunk;        }); Resp.on (' End ',function(){            //Create a directoryFs.mkdir ('./' +subfolder,function(err) {if(Err) {Console.log ("Directory" +folder+ "already exists");            }            }); //Create a file            varFilename= "./" +subfolder+ "/" +pinctureindex+ ". jpg"; Fs.writefile (FileName, Imgdata,"Binary",function(err) {if(Err) {Console.log ("File" +filename+ "Download failed.");                Console.log (ERR); }Else{Console.log ("File" +filename+ "Download succeeded");            }            });    });    }); //Timeout ProcessingReq.settimeout (5000,function() {req.abort ();    }); //Error HandlingReq.on (' Error ',function(err) {if(err.code== "Econnreset") {Console.log (' [Downloadpic]socket port connection timed out. ‘);        Console.log (ERR); }Else{Console.log (' [Downloadpic] Request error occurred, Err.code: ' +Err.code);        Console.log (ERR);    }    }); //End of Requestreq.end ();}/*left auto complement 0 rustic live by Lifesinger*/  functionpad (num, n) {varLen =num.tostring (). length;  while(Len <n) {num= "0" +num; Len++; }      returnnum; } //Download a batchfunctionstart () { for(vari=1;i<55;i++){        varFilename=pad (i,3); Downloadpic (' 11519 ', ' 11602 ', FileName); }}//call the Start function and the program startsStart ();

node. js nvshens Image bulk Download crawler 1.00

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.