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);
    })
原文地址:https://www.cnblogs.com/cart55free99/p/4780152.html