Node爬虫之初体验

记得之前就听说过爬虫,个人初步理解就是从网页中抓取一些有用的数据,存储到本地,今天就当是小牛试刀,拿来溜溜......

实现需求抓取课程数据,输入url后并在浏览器端以一定的数据格式显示出来(如下图所示)

    实现需求需用到的Node库介绍

cheerio(https://github.com/cheeriojs/cheerio )  可以理解成一个 Node.js 版的 jquery用来从网页中以 css selector 取数据,使用方式跟 jquery 一样一样的。

superagent(http://visionmedia.github.io/superagent/ ) 是个轻量的的 http 方面的库,是nodejs里一个非常方便的客户端请求代理模块,当我们需要进行 get 、 post 、 head 等网络请求时

express(http://www.expressjs.com.cn/starter/) 是一个基于 Node.js 平台的极简、灵活的 web 应用开发框架,路由、express生成器、静态文件等。

    实现需求源代码如下

package.json  

npm init生成package.json配置文件

devDependencies、dependencies 依赖组件

{
  "name": "package.json",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "cheerio": "^0.22.0"
  },
  "devDependencies": {
    "express": "^4.15.2",
    "superagent": "^3.5.0"
  },
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "Avenstar",
  "license": "ISC"
}

crawler.js

var express = require('express'),
    app = express(),//基于WEB平台的开发框架
    superagent = require("superagent"),//处理服务端/客户端的http请求
    cheerio=require('cheerio');//一个 Node.js 版的 jquery,用来从网页中以 css selector 取数据,使用方式跟 jquery 一样
var pathUrl='http://www.imooc.com/learn/348';
  
/*=========================================================================
|抓取data数据结构如下
|    var courseData = [{
|           chapterTitle:'',
|            videos:[{
|              title:'',
|               id:''
|            }]
|     }]
*==========================================================================*/
function printCourseInfo(courseData){
    courseData.forEach(function(item){
        var chapterTitle=item.chapterTitle;
        console.log(chapterTitle+'
');
        item.videos.forEach(function(video){
            console.log(' 【'+video.id+'】'+video.title+'
');
        })
    });
}
/*==========================================================================
|   分析从网页里抓取到的数据
==========================================================================*/
function filterChapter(html){
    var courseData=[];
    var $=cheerio.load(html);
    var chapters=$('.chapter');
    chapters.each(function(item){
        var chapter=$(this);
        var chapterTitle=chapter.find('strong').text().replace(/(s*)/g,''); //找到章节标题
        var videos=chapter.find('.video').children('li');
  
        var chapterData={
            chapterTitle:chapterTitle,
            videos:[]
        };
        
        //videos
        videos.each(function(item){
            var $that = $(this),
                video=$that.find('.J-media-item'),
                title=video.text().replace(/(s*)/g,'');
                id=video.attr('href').split('/video')[1].replace(/(s*)/g,'').replace('/','');
            chapterData.videos.push({
                title:title,
                id:id
            })
        })
        courseData.push(chapterData);
    });  
    return courseData;
}
/*==========================================================================
| GET method route
===========================================================================*/
app.get('/', function(request, respones){
   //处理服务端/客户端的http请求
   superagent.get(pathUrl).end(function(error, sres){
       //error
       if(error){
          return next(err);
       }
       //抓取https网址html
       var html = sres.text;
       var courseData=filterChapter(html);
       //打印
       printCourseInfo(courseData);
       //respones
       respones.send((courseData));
    })
})
/*==========================================================================
| listening at port
===========================================================================*/
app.listen(9090, function(){
    console.log('app is listening at port 9090');
});
  

资料参考

 http://www.imooc.com/video/7965

   http://www.cnblogs.com/coco1s/p/4954063.html

   https://github.com/alsotang/node-lessons

作者:Avenstar

出处:http://www.cnblogs.com/zjf-1992/p/6548220.html

关于作者:专注于前端开发

本文版权归作者所有,转载请标明原文链接

原文地址:https://www.cnblogs.com/zjf-1992/p/6548220.html