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

    Node爬虫

    参考
    http://www.cnblogs.com/edwardstudy/p/4133421.html

    所谓的爬虫就是发送请求,并将响应的数据做一些处理
    只不过不用浏览器来发送请求

    需要的模块

    • superagent
    • url (解析url用 因为在node中没有document)
    • cheerio (将文本解析为JQ的DOM对象)
      其它
      q(promise) eventproxy

    superagent

    SuperAgent 是一个轻量的Ajax API,服务器端(Node.js)客户端(浏览器端)均可使用, 可作为客户端请求代理模块使用,当你想处理get,post,put,delete,head请求时,可以考虑使用SuperAgent

    http://www.tuicool.com/articles/MbEnQfQ
    https://visionmedia.github.io/superagent/

    获取网页数据

    var targetUrl = 'https://cnodejs.org/';
    
    function getRawData(){
        return Q.promise(function(resolve, reject){
            superagent.get(targetUrl)
                .end(function(err, res) {
                    if(err){
                        reject(err);
                    }else{
                        //console.log(res);//返回的格式包含Header   Response Connection等很多信息 //而网页本身的内容则在text中
                        resolve(res);
                    }
                });
        });
    }
    
    

    解析

    getRawData().then(function(res){
        // console.log(res.text);
        return cheerio.load(res.text);  //之后返回的是一个经过JQ的body对象 如同$(document.body)一样
    }, function(err){
        console.log(err);
    }).then(function($){
        var topicUrls = [];
        $('#topic_list .topic_title').each(function(idx,ele){
            ele = $(ele);
            var href = url.resolve(targetUrl, ele.attr('href'));
            topicUrls.push(href);
        });
        return topicUrls;
    })
    

    More

    .then(function(urls){
        var ep = new eventproxy();
        //这里使用Promise.all 也是Ok的
        //enevtProxy 可以再监听到指定次数的事件后触发
        ep.after('topic', urls.length, function(topics){
            topics = topics.map(function(item){
                var text = item.text;
                var $ = cheerio.load(item.text);
                return {
                    href: item.url,
                    title: $('.topic_full_title').html() ? $('.topic_full_title').html() : '',
                    comment: $('.reply_content').eq(0).text() ? $('.reply_content').eq(0).text() : ''
                }
            });
            console.log(topics);
        });
        urls.forEach(function(url){
            superagent.get(url).end(function(err, res){
                ep.emit('topic', {
                    url:url,
                    text: res.text
                });
            })
        });
    });
    

    使用Promise的话

        var promises = urls.map(function(url){
            return Q.promise(function(resolve, reject){
                superagent.get(url).end(function(err,res){
                    resolve({
                        url: url,
                        text: res.text
                    });
                });
            });
        });
        Q.all(promises).then(function(arr){
            console.log('-------------all-------------');
            arr = arr.map(function(item){
                var $ = cheerio.load(item.text);
                return {
                    href: item.url,
                    title: $('.topic_full_title').html() ? $('.topic_full_title').html() : '',
                    comment: $('.reply_content').eq(0).text() ? $('.reply_content').eq(0).text() : ''
                }
            });
            console.log(arr);
        })
    
  • 相关阅读:
    log4j(七)——log4j.xml简单配置样例说明
    log4j(六)——log4j.properties简单配置样例说明
    三元运算符注意事项
    单精度浮点数操作
    反转链表算法Java实现
    VBS计时器2
    肖申克的救赎影评
    计算机中K到底是1000还是1024?
    二进制补码除法——计算机底层整数除法模拟之Java实现
    VBS计时器
  • 原文地址:https://www.cnblogs.com/cart55free99/p/4780152.html
Copyright © 2011-2022 走看看