zoukankan      html  css  js  c++  java
  • Node 简单爬虫

    以爬慕课网Hadoop进阶课程为例,用Node写一个简单的爬虫:

    先抓取这个网站的源码:

    var http = require('http');
    var url = 'http://www.imooc.com/learn/890';
    
    http.get(url, function(res) {
        var html = '';
    
        res.on('data', function(data) {
            html += data;
        })
    
        res.on('end', function() {
            console.log(html);
        })
    }).on('error', function() {
        console.log('获取课程数据出错!')
    })

     然后分析这个页面的Dom,如图:

    每大章节都被一个chapter包围,抓取下来就是一个数组,对每个item,这张的大标题在strong里面,每章的小章节在video标签里,然后小标题就是J-media-item的text,id就用video的编号,字符串截取下来。

    代码:

    var http = require('http');
    var cheerio = require('cheerio')
    var url = 'http://www.imooc.com/learn/890';
    
    function filterChapters(html) {
        var $ = cheerio.load(html)
        var chapters = $('.chapter')
    
        // [{
        //     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('.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 + 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('获取课程数据出错!')
    })

     当然爬下来的结果格式有点杂乱无章:

    可以在加点代码格式化一下,也可以用IO写进磁盘文件。

  • 相关阅读:
    ArrayList集合 、特殊集合
    二维数组 、多维数组
    一维数组
    类:String,Math,DateTime,Random
    while做法1.兔子生兔子 2.求100以内质数的和3.洗发水15元 牙膏5元 香皂2元 150元的算法
    博客迁移
    [WC2008]游览计划 「斯坦那树模板」
    [SDOI2009]HH去散步 「矩阵乘法计数」
    [HNOI2007]梦幻岛宝珠 「套路:分层 $DP$」
    多项式求逆
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7587577.html
Copyright © 2011-2022 走看看