zoukankan      html  css  js  c++  java
  • nodeJs编写小爬虫

    var http=require('http');
    var cheerio=require('cheerio');
    var url='http://www.XXXX';

    function filterChapters(html){
        var $=cheerio.load(html);
        var chapters=$('.chapter');//不是 .learnchapter了
        // [{
        //     chapterTitle:'';
        //     videos:[
        //         title:'';
        //         id='';
        //     ]
        // }]
        var courseData=[];
        chapters.each(function(item){
            var chapter=$(this);
            var chapterTitle=chapter.find('strong').text();
            var videos=chapter.find('.video').children('li');
            var chapterData={
                chapterTitle:chapterTitle,
                videos:[]
            }
            videos.each(function(item){
                var video=$(this).find('.studyvideo');
                var videoTitle=video.text();
                var id=video.attr('href').split('video/')[1];
                chapterData.videos.push({
                    title:videoTitle,
                    id:id
                });
            });
            courseData.push(chapterData);
    })

    return courseData;
    }

    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+' ');
            });
        });
    }

    http.get(url,function(res){
        var html='';
        res.on('data',function(data){
            html+=data;
        })
        res.on('end',function(){
            var courseData=filterChapters(html);
            printCourseInfo(courseData);
        })
    }).on('error',function(){
        console.log('获取课程数据失败!')
    })

  • 相关阅读:
    django QQ认证登录
    python mixin到底是什么 django
    Django View类的解析
    [置顶] 十道海量数据处理面试题
    Epoll简介以及例子
    GCC在C语言中内嵌汇编 asm __volatile__
    在FireBug中计算Javascript 运行时间
    Two Sum
    Longest Consecutive Sequence
    Evaluate Reverse Polish Notation
  • 原文地址:https://www.cnblogs.com/aliwa/p/6359622.html
Copyright © 2011-2022 走看看