Node. JS uses an HTTP client to download a picture of the site to a local

Source: Internet
Author: User

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

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.