nodeJs爬蟲獲取數據簡單實現(xiàn)代碼
來源:易賢網 閱讀:1111 次 日期:2016-07-15 14:19:07
溫馨提示:易賢網小編為您整理了“nodeJs爬蟲獲取數據簡單實現(xiàn)代碼”,方便廣大網友查閱!

這篇文章主要為大家詳細介紹了nodeJs爬蟲獲取數據簡單實現(xiàn)代碼,感興趣的小伙伴們可以參考一下

本文實例為大家分享了nodeJs爬蟲獲取數據代碼,供大家參考,具體內容如下

var http=require('http');

var cheerio=require('cheerio');//頁面獲取到的數據模塊

var url='http://www.jcpeixun.com/lesson/1512/';

function filterData(html){

  /*所要獲取到的目標數組 

   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)

  })

})

以上就是nodeJs爬蟲獲取數據的相關代碼,希望對大家的學習有所幫助。

更多信息請查看網絡編程

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網