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('获取课程数据失败!')
    })

  • 相关阅读:
    ios后台运行
    关于CRM2011插件注册更改记录状态的消息
    Microsoft Dynamics CRM 4.0字段审核跟踪功能
    CRM2011CTI集成项目
    win7下VS2010、IIS7配置常见问题收集
    CRM2011共享记录、更改状态、分派记录
    转载 [Dynamics CRM]錯誤代碼參考
    准备开发CRM2011的重要前提
    Aspx引用RDL
    CRM2011开发小技巧
  • 原文地址:https://www.cnblogs.com/aliwa/p/6359622.html
Copyright © 2011-2022 走看看