HTTP Source code Interpretation
HTTP Performance Test
1 varHTTP = require (' http ');2 3 http4. Createserver (function(Request, response) {5Response.writehead, {' Content-type ': ' Text/plain '});6Response.Write (' Hello Nodejs ');7 Response.End ();8 })9. Listen (2016);Ten OneConsole.log (' Success '); A - //Access localhost:2016 will see the return of Hello Nodejs
HTTP small Reptile
Installing Cheerio
NPM Install Cheerio
/**********************************/
1 varHTTP = require (' http ');2 varurl = ' http://www.imooc.com/learn/348 ';3 4Http.get (URL,function(response) {5 varhtml = ' ';6 7Response.on (' Data ',function(data) {8HTML + =data;9 });Ten OneResponse.on (' End ',function () { A Console.log (HTML); - }); -}). On (' Error ',function () { theConsole.log (' Error getting course data ')); -});
/**********************************/
1 /**2 * Nodejs crawler instances, crawl Imooc*/3 4 varHTTP = require (' http ');5 //install Cheerio, and bring in6 varCheerio = require (' Cheerio '));7 varurl = ' http://www.imooc.com/learn/348 ';8 9 functionFilterchapter (HTML) {Ten var$ =cheerio.load (HTML); One varChapters = $ ('. Chapter ')); A - //[{ - //chaptertitle: ", the //Videos: [ - //title: ", - //ID: " - // ] + //}] - + varCoursedata = []; A atChapters.each (function(item) { - varChapter = $ ( This); - varChaptertitle = Chapter.find (' strong ')). text (); - varVideos = Chapter.find ('. Video '). Children (' Li ')); - - varChapterdata = { in Chaptertitle:chaptertitle, - videos: [] to }; + -Videos.each (function(item) { the varVideo = $ ( This). Find (' a '); * varVideotitle =Video.text (); $ varid = video.attr (' href '));Panax Notoginseng - ChapterData.videos.push ({ the Videotitle:videotitle, + Id:id A }); the }); + //Console.log (chapterdata); - $ Coursedata.push (chapterdata); $ }); - //Console.log (coursedata); - //Console.log (Coursedata.videos); the - /*Coursedata.foreach (function (item) {Wuyi //console.log (Item.videos); the var videotitle = item.videos[0].videotitle; - var id = item.videos[0].id; Wu console.log (' [' + ID + '] ' + videotitle); - });*/ About $ returnCoursedata; - } - - functionPrintcourseinfo (coursedata) { A //Coursedata is an array +Coursedata.foreach (function(item) { the varChaptertitle =Item.chaptertitle; -Console.log (chaptertitle + ' \ n ')); $ }); the theCoursedata.foreach (function(item) { the varVideotitle = Item.videos[0].videotitle; the varid = item.videos[0].id; -Console.log (' [' + ID + '] ' +videotitle); in }); the //console.log (' test '); the //Console.log (Coursedata.videos); About } the theHttp.get (URL,function(response) { the varhtml = ' '; + -Response.on (' Data ',function(data) { theHTML + =data;Bayi }); the theResponse.on (' End ',function () { - varCoursedata =filterchapter (HTML); - the Printcourseinfo (coursedata); the //Console.log (coursedata); the }); the}). On (' Error ',function () { -Console.log (' Error getting course data ')); the});
node. JS (iv) "HTTP crawler"