//======================================================//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