Home > Web Front-end > JS Tutorial > body text

Simple implementation code for nodeJs crawler to obtain data_node.js

WBOY
Release: 2016-05-16 15:07:33
Original
1680 people have browsed it

The example in this article shares the nodeJs crawler data acquisition code for your reference. The specific content is as follows

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)
  })
})
Copy after login

Rendering:

The above is the relevant code for nodeJs crawler to obtain data. I hope it will be helpful to everyone's learning.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!