2013-11-26 66 views
0

有沒有人使用Node Cheerio刮擦整個網站,而不僅僅是刮板指向的home/first page?Node Cheerio颳去整個網站

在這一刻我正在做的只是刮目標網頁。

request('http://arandomsite.com/', function (error, response, html) { 
    if (!error && response.statusCode == 200){ 
     var $ = cheerio.load(html); 
      ... 
      ... 
      ... 
}; 

回答

1

我從來沒有使用Cheerio,但我會假設(如可能與其他刮板),它只會做你指向它的頁面。假設cheerio.load返回類似API jQuery的,你可能會不得不做一些像

$('a').each(function(index, a) { 
    //TODO: You may want to keep track here of which you have done, and not redo any. 
    request('http://arandomsite.com' + a.attr('href'), myPageProcessFunction); 
}); 

很明顯,你將需要添加的東西像I幀以及確保你得到一個完整的結果。

爲了澄清,這裏是一些更新的代碼:

request('http://arandomsite.com/', function responseFunction(error, response, html) { 
if (!error && response.statusCode == 200){ 
    var $ = cheerio.load(html); 
    $('a').each(function(index, a) { 
     request('http://arandomsite.com' + a.attr('href'), responseFunction); 
    }); 
}; 
}); 
+0

與此唯一的問題是請求的功能是直接低於我的節點相關性的變量,因此一個問題,我可以看到的是,通過包裝$('a')。each(function(index,a){};'中的請求會導致$是未定義的 – leaksterrr

+1

我不是說要包裝請求,這段代碼應該在$當我說每個鏈接上的呼叫請求,我的意思是做一個新的請求調用,並傳遞當前函數作爲回調結果。 –

+0

我看到你的邏輯和感謝更新的代碼,這是有道理的。唯一的問題我沒有那個它是說a沒有方法'attr'?這是一個鏈接到一個完整的代碼,以讓您更好地瞭解我在做什麼http://pastie.org/private/snykxn92q23ga8srnpak3a#1 – leaksterrr