This article mainly introduces the simple implementation code of node. js Crawlers for getting data. Interested partners can refer to the examples in this article to share with you the node. js crawler data code for your reference, the details are as follows:
Var http = require ('http'); var cheerio = require ('cheerio '); // the data module var url obtained on the page =' http://www.jcpeixun.com/lesson/1512/ '; Function filterData (html) {/* target array to be obtained var courseData = [{chapterTitle: "", videosData: {videoTitle: title, videoId: id, videoPrice: price}] */var $ = cheerio. load (html); var courseData = []; var chapters = $ (". list-collapse "); chapters. each (function (item) {var chapterTitle = $ (this ). find (". collapse-head "). find ("label "). text (); var videos = $ (this ). find (". listview5 "). children ("li"); var chaptersData = {chaptersTitle: chapterTitle, videosData: []} videos. each (function (item) {var videoTitle = $ (this ). find (". ml10 "). attr ('data-lesson-name'); var videoId = $ (this ). find (". ml10 "). attr ('data-lesson-id'); var vadeoPrice = $ (this ). find (". colblue "). text (); chaptersData. videosData. push ({title: videoTitle, id: videoId, price: vadeoPrice}) courseData. push (chaptersData)}) return courseData} function printCourseInfo (courseData) {courseData. forEach (function (item) {console. log (item. chaptersTitle + '\ n'); item. videosData. forEach (function (item) {console. log (item. title + '[' + item. id + ']' + item. price + '\ n')} http. get (url, function (res) {html = ""; res. on ("data", function (data) {html + = data}) res. on ('end', function () {var courseData = filterData (html); printCourseInfo (courseData )})})
:
The above is the code used by the node. js crawler to obtain data. I hope it will be helpful for your learning.