The following code is a bit redundant, everyone pick the core to see just FINE.
//built-in HTTP module that provides HTTP server and client functionalityvarHttp=require ("http");//Cheerio module with jquery-like functionalityvarCheerio = Require ("cheerio");//built-in file processing modulevarFs=require (' FS ');//Request parameter JSONvaroptions;//request and obtain datavarreq;//Store Page NumbersvarIndex=4;functiondownloadimg (pagenumber) {console.log ("start reading page" +pagenumber+ "); //http://images2017.cnblogs.com/blog/162475/201709/162475-20170929115047356-360094413.png//actual Picture Addressoptions={hostname:' Images2017.cnblogs.com ',//do not add http.//, otherwise there will be Enotfound errorsPort:80, Path:'/blog/162475/201709/162475-20170929115047356-360094413.png ',//Sub-pathMethod: ' GET ', }; Req=http.request (options,function(resp) {varImgdata = ""; Resp.setencoding ("binary"); Resp.on (' Data ',function(chunk) {imgdata+=chunk; }); Resp.on (' End ',function() {fs.writefile ("162475-20170929115047356-360094413.png", imgdata, "binary",function(err) {if(err) {console.log ("file Download failed."); } console.log ("download succeeded"); }); }); }); //Timeout ProcessingReq.settimeout (5000,function() {req.abort (); }); //Error HandlingReq.on (' Error ',function(err) {if(err.code== "econnreset") {console.log (' Socket port Connection timed Out. ‘); }Else{console.log (' Request error occurred, err.code: ' +err.code); } }); //End of Requestreq.end ();}//wrapping a layer of functionsfunctionstart (i) {downloadimg (i);}//Start TraversalStart (index);
September 30, 2017 10:31:31
Node. JS uses HTTP clients to download pictures of Web sites to local