zoukankan      html  css  js  c++  java
  • node小爬虫

    这一章主利用node的http模块制作一个网页的小爬虫来爬去网页信息,其中对于后端html的节点的获取采用了cheerio模块,这

    /**
     * Created by Administrator on 2016/9/16.
     */
    var http = require('http');
    var cheerio = require('cheerio');
    var url = 'http://www.imooc.com/learn/348';
    function filterChapters(html){
          var $ = cheerio.load(html);// 要使用cheerio模块先要用npm install cheerio加载进来,然后再前面引入(var cheerio = require('cheerio');)
          var chapters = $('.chapter');
    
        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('.J-media-item');
                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)
                //console.log(' 【'+ video.id + '】 '+ video.title + '
    ');
    
            })
        })
    }
    
    http.get(url,function(res){
        var html = '';
        res.on('data',function(data){ res会监听data事件的发生
            html += data;
        });
        res.on('end',function(){
    
            var courseData = filterChapters(html);
    
            printCourseInfo(courseData);
        })
    }).on('error',function(){
        console.log('获取课程出错!')
    })

    个模块可以在后端获取html页面的元素

    ,获取方法类似于jquery

    代码如下

  • 相关阅读:
    【POJ 2044】 Weather Forecast
    【POJ 1703】 Find them,Catch them
    【SCOI 2005】 骑士精神
    字长与指针
    XModem协议
    SecureCRT乱码问题解决方法
    usb设备驱动程序
    如何检测 51单片机IO口的下降沿
    matlab神经网络工具箱创建神经网络
    九针串口接线问题, 232, 485
  • 原文地址:https://www.cnblogs.com/yuaima/p/5876627.html
Copyright © 2011-2022 走看看