node js 小爬虫

 
  node js 小爬虫 
 
var http = require('http');
var cheerio = require('cheerio');
var url = "http://www.imooc.com/learn/348";
var baseurl= "http://www.imooc.com/learn/";
var vediosId=[637,348,259,197,134,75];
var Promise=require('bluebird')

function filterChapters(html) {
    var $ = cheerio.load(html);
    var chapters = $('.chapter');

    var title = $('.course-infos .path span').text();

    var number = parseInt($($('.meta-value strong')[3]).text().trim(), 10);

    var courseData ={
        couseTitle:title,
        number:number,
        videos:[]
    };

    var chapter;
    var chapterTitle;
    var videos;
    var chapterData;
    var video;
    var videoTitle;
    var id;
    var adress;

    chapters.each(function(index, item) {
        chapter = $(item);
        chapterTitle = chapter.find('strong').text();
        videos = chapter.find('li');
        chapterData = {
            'chapterTitle': chapterTitle,
            'videos': []
        };
        videos.each(function(index, item) {
            video = $(item).find('.J-media-item');
            videoTitle = video.text();
            id = video.attr('href').split('video/')[1];
            adress = video.attr('href');

            chapterData.videos.push({
                'title':videoTitle,
                'id':id,
                'adress':adress
            });
        });
        courseData.videos.push(chapterData);
    });
    return courseData;
}

function printCourseInfo(coursesData) {

    var chapterTitle;
    var urlPre = 'URL:http://www.imooc.com';
    coursesData.forEach(function(courseData) {
        chapterTitle = courseData.couseTitle;
        console.log('@@'+chapterTitle + ':' + courseData.number+'人学过!'+ '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n');
        courseData.videos.forEach(function(item){
            console.log('###'+item.chapterTitle);
            item.videos.forEach(function(t){
                console.log('   '+t.title+' '+ t.id+' '+ t.adress+'\n');
            })
        })
    });
}
function getPageasync(url){
    return new Promise(function(resolve,reject){
        console.log(url);
        http.get(url, function(res) {
            var html = '';
            res.on('data', function(data) {
                html += data;
            });

            res.on('end', function() {
                resolve(html);
            });
        }).on('error', function(e) {
            reject(e);
            console.log("失败");
        });
    })
}


var fecthCourseArray=[];
vediosId.forEach(function(id){
    fecthCourseArray.push(getPageasync(baseurl+id))
});

Promise.all(fecthCourseArray).then(function(pages){
    var cousesData=[];
    pages.forEach(function(html){
        var couseData=filterChapters(html);
        cousesData.push(couseData)
    });
    cousesData.sort(function(a,b){
        return a.number< b.number
    });
    printCourseInfo(cousesData)
});


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值