1 //http Small Reptile2 //xxxxx similar to $. Ajaxxxxxxx3 4 5 varHTTP = require (' http ');//Loading HTTP Modules6 7 varCheerio = require (' Cheerio ');//loading third-party modules cheerio like jquery8 9 //Installation Method NPM Install CheerioTen One A functionPrintcourseinfo (Coursedata) {//Print function incoming fetch data -Coursedata.foreach (function(item) {//Cycle Printing - varChaptertitle =Item.chaptertitle; the -Console.log (chaptertitle + ' \ n ')); - -Item.videos.forEach (function(video) { +Console.log (' [' + video.id + '] ' + video.title + ' \ n '); - }) + }) A } at - - functionFliterchapters (HTML) {//Data Filter Functions - - var$ =cheerio.load (HTML); - in varChapters = $ ('. Chapter ')//get Element - to + //Target data Structure - /* [{ the captertitle: ", * videos: ", $ ID: "Panax Notoginseng }]*/ - the varCoursedata = [];//Storing Arrays + A theChapters.each (function(item) { + varChapters = $ ( This); - $ varChaptertitle = Chapters.find (' strong ')). text (); $ - - varVideos = Chapters.find ('. Video '). Children (' Li ')); the - varChapterdata = {Wuyi Chaptertitle:chaptertitle, the videos: [] - } Wu -Videos.each (function(item) { About varVideo = $ ( This). Find ('. J-media-item '); $ varVideotitle =Video.text (); - varid = video.attr (' href '). Split (' video/') [1] - - A ChapterData.videos.push ({ + Title:videotitle, the Id:id - }) $ }) the the the Coursedata.push (chapterdata); the }) - in returnCoursedata//data stitching completed and returned the the } About the //Destination URL the varurl = ' http://www.imooc.com/learn/348 ';//Mu class net the + - the //using the Get methodBayiHttp.get (URL,function(RES) {//Get method Crawl substitution code the varhtml = ' '; the -Res.on (' Data ',function(data) {//Get Data Events -HTML + =data; the }) the theRes.on (' End ',function() {//Get End Event the varCoursedata =fliterchapters (HTML); - the Printcourseinfo (coursedata); the }) the 94}). On (' Error ',function() { theConsole.log (' Get Error! ‘);//Error the})
Beginner Nodejs---http crawler