node. JS (iv) "HTTP crawler"

Source: Internet
Author: User

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"

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.