大致思路:
請(qǐng)求一個(gè)頁(yè)面接收到頁(yè)面的html代碼,解析代碼拿到想要的數(shù)據(jù)。
我們以抓取googlePlay上2016年度最佳游戲Pokémon GO的評(píng)論為例,寫一個(gè)簡(jiǎn)單的爬蟲
- 獲取頁(yè)面html代碼
var request=require('request');
var url='https://play.google.com/store/apps/details?id=com.nianticlabs.pokemongo&hl=zh_CN';
request(url,(error, response, body)=>{
console.log(body);
})
node 運(yùn)行后,控制臺(tái)打印如下內(nèi)容:

body打印結(jié)果
- 篩選需要的數(shù)據(jù)
這個(gè)步驟中可以用到cheerio來解析html,用jquery的方式就可以輕松取到內(nèi)容了,很方便。
然后我們用chorme的開發(fā)者工具看一下單個(gè)評(píng)論的結(jié)構(gòu),選擇其中紅框后面的數(shù)據(jù)進(jìn)行抓取

評(píng)論結(jié)構(gòu)分析
給之前的代碼添加一個(gè)解析順便打印的函數(shù)
function formatHtml(html){
var $=cheerio.load(html);
var reveiws=$('.single-review');
var reviewList=[];
reveiws.each(function(item){
var reveiws=$(this);
var author=reveiws.find('.author-name').text();
var star=reveiws.find('.tiny-star').attr('aria-label');
var rewTitle=reveiws.find('.review-title').text();
var rewContent=reveiws.find('.review-body').text();
console.log('rewTitle',rewTitle);
console.log('\n author:',author,' star:',star);
console.log('\n rewContent:',rewContent);
console.log('-------------------------------------');
})
}
在之前打印body的地方調(diào)用formatHtml函數(shù),傳入body
request(url,(error, response, body)=>{
formatHtml(body);
})
運(yùn)行文件,就可以看到打印結(jié)果了:

Pokémon GO評(píng)論結(jié)果(部分)
好了,最基本的抓取就是這樣,抓不同內(nèi)容也大同小異,試一下吧~
抓完才發(fā)現(xiàn)評(píng)價(jià)并不太好哈~哈哈,可以把url最后的語(yǔ)言改成ja或者其他語(yǔ)言看看別的區(qū)的評(píng)論,就醬~