Code _ node. js

Source: Internet
Author: User
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.

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.