Beginner Nodejs---http little crawler

Source: Internet
Author: User

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

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.