建立項(xiàng)目craelr-demo
我們首先建立一個(gè)Express項(xiàng)目,然后將app.js的文件內(nèi)容全部刪除,因?yàn)槲覀儠簳r(shí)不需要在Web端展示內(nèi)容。當(dāng)然我們也可以在空文件夾下直接 npm install express
來(lái)使用我們需要的Express功能。
目標(biāo)網(wǎng)站分析
如圖,這是CNode首頁(yè)一部分div標(biāo)簽,我們就是通過(guò)這一系列的id、class來(lái)定位我們需要的信息。
使用superagent獲取源數(shù)據(jù)
superagent就是ajax API來(lái)使用的Http庫(kù),它的使用方法與jQuery差不多,我們通過(guò)它發(fā)起get請(qǐng)求,在回調(diào)函數(shù)中輸出結(jié)果。
代碼如下:
var express = require('express');
var url = require('url'); //解析操作url
var superagent = require('superagent'); //這三個(gè)外部依賴不要忘記npm install
var cheerio = require('cheerio');
var eventproxy = require('eventproxy');
var targetUrl = 'https://cnodejs.org/';
superagent.get(targetUrl)
.end(function (err, res) {
console.log(res);
});
它的res結(jié)果為一個(gè)包含目標(biāo)url信息的對(duì)象,網(wǎng)站內(nèi)容主要在其text(string)里。
使用cheerio解析
cheerio充當(dāng)服務(wù)器端的jQuery功能,我們先使用它的.load()來(lái)載入HTML,再通過(guò)CSS selector來(lái)篩選元素。
代碼如下:
var $ = cheerio.load(res.text);
//通過(guò)CSS selector來(lái)篩選數(shù)據(jù)
$('#topic_list .topic_title').each(function (idx, element) {
console.log(element);
});
其結(jié)果為一個(gè)個(gè)對(duì)象,調(diào)用 .each(function(index, element))
函數(shù)來(lái)遍歷每一個(gè)對(duì)象,返回的是HTML DOM Elements。
輸出 console.log($element.attr('title'));
的結(jié)果為 廣州 2014年12月06日 NodeParty 之 UC 場(chǎng)
之類的標(biāo)題,輸出 console.log($element.attr('href'));
的結(jié)果為 /topic/545c395becbcb78265856eb2
之類的url。再用NodeJS1的url.resolve()函數(shù)來(lái)補(bǔ)全完整的url。
代碼如下:
superagent.get(tUrl)
.end(function (err, res) {
if (err) {
return console.error(err);
}
var topicUrls = [];
var $ = cheerio.load(res.text);
// 獲取首頁(yè)所有的鏈接
$('#topic_list .topic_title').each(function (idx, element) {
var $element = $(element);
var href = url.resolve(tUrl, $element.attr('href'));
console.log(href);
//topicUrls.push(href);
});
});
使用eventproxy來(lái)并發(fā)抓取每個(gè)主題的內(nèi)容
教程上展示了深度嵌套(串行)方法和計(jì)數(shù)器方法的例子,eventproxy就是使用事件(并行)方法來(lái)解決這個(gè)問(wèn)題。當(dāng)所有的抓取完成后,eventproxy接收到事件消息自動(dòng)幫你調(diào)用處理函數(shù)。
代碼如下:
//第一步:得到一個(gè) eventproxy 的實(shí)例
var ep = new eventproxy();
//第二步:定義監(jiān)聽(tīng)事件的回調(diào)函數(shù)。
//after方法為重復(fù)監(jiān)聽(tīng)
//params: eventname(String) 事件名,times(Number) 監(jiān)聽(tīng)次數(shù), callback 回調(diào)函數(shù)
ep.after('topic_html', topicUrls.length, function(topics){
// topics 是個(gè)數(shù)組,包含了 40 次 ep.emit('topic_html', pair) 中的那 40 個(gè) pair
//.map
topics = topics.map(function(topicPair){
//use cheerio
var topicUrl = topicPair[0];
var topicHtml = topicPair[1];
var $ = cheerio.load(topicHtml);
return ({
title: $('.topic_full_title').text().trim(),
href: topicUrl,
comment1: $('.reply_content').eq(0).text().trim()
});
});
//outcome
console.log('outcome:');
console.log(topics);
});
//第三步:確定放出事件消息的
topicUrls.forEach(function (topicUrl) {
superagent.get(topicUrl)
.end(function (err, res) {
console.log('fetch ' + topicUrl + ' successful');
ep.emit('topic_html', [topicUrl, res.text]);
});
});
結(jié)果如下
擴(kuò)展練習(xí)(挑戰(zhàn))
獲取留言用戶名和積分
在文章頁(yè)面的源碼找到評(píng)論的用戶class名,classname為reply_author。console.log第一個(gè)元素 $('.reply_author').get(0)
可以看到,我們需要獲取東西都在這里頭。
首先,我們先對(duì)一篇文章進(jìn)行抓取,一次性把需要的都得到即可。
代碼如下:
var userHref = url.resolve(tUrl, $('.reply_author').get(0).attribs.href);
console.log(userHref);
console.log($('.reply_author').get(0).children[0].data);
我們可以通過(guò)https://cnodejs.org/user/username
抓取積分信息
代碼如下:
$('.reply_author').each(function (idx, element) {
var $element = $(element);
console.log($element.attr('href'));
});
在用戶信息頁(yè)面 $('.big').text().trim()
即為積分信息。
使用cheerio的函數(shù).get(0)為獲取第一個(gè)元素。
代碼如下:
var userHref = url.resolve(tUrl, $('.reply_author').get(0).attribs.href);
console.log(userHref);
這只是對(duì)于單個(gè)文章的抓取,對(duì)于40個(gè)還有需要修改的地方。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com