2016-08-24 157 views
0

我是新來的NightmareJS並寫了一個腳本來刮一個網站。 這樣工作。我登錄到我的個人資料,等待網站加載,然後去我喜歡的個人資料,我想向下滾動,直到網站結束。目前我使用這種醜陋的工作,並想知道是否有辦法向下滾動到頁面底部以獲取所有結果,然後進入下一步。懶惰裝載滾動與噩夢JS

var Nightmare = require('nightmare'); 
var vo = require('vo'); 
vo(run)(function(err, result) { 
    if (err) throw err; 
}); 
function *run() { 
    var nightmare = Nightmare({ show: true, 
          webPreferences: { partition: 'your-custom-partition'}}); 
    yield nightmare 
    .goto('https://facebook.com/login') 
    .type('input[id="email"]', "user") 
    .type("input[id='pass']", "pass") 
    .click('#loginbutton') 
    .wait('._8u._42ef') 
    .goto('https://www.facebook.com/myprofile/likes') 
    .wait(1000) 
    yield nightmare 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
     .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
     .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
     .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    .evaluate(function() { 
     window.document.body.scrollTop = document.body.scrollHeight; 
    }) 
    .wait(3000) 
    var title = yield nightmare 
    .evaluate(function() { 
       var jsonObject = new Array(''); 
       var links = document.getElementsByClassName("_5rz _5k3a _5rz3 _1v6c"); 
       var numProfiles = links.length; 
       for(var i = 0; i< numProfiles; i++){ 
       var elem; 
       elem = links[i].querySelector(".fsl.fwb.fcb a").href; 
       console.log(elem); 
       jsonObject.push(elem); 
       } 
       if(numProfiles > 0) { 
       //then delete that element, so we don't overlaod the page 
       for(var j = 0; j < numProfiles; j++){ 
       links[0].parentNode.removeChild(links[0]); 
       } 
       window.document.body.scrollTop = document.body.scrollHeight; 
       } 
       return jsonObject; 
    }); 
    console.log(title); 
    yield nightmare.end(); 
} 

回答

1

我想你以後類似this answer的東西,這是關係到segmentio/nightmare#625

爲了完整起見,下面包含了參考答案中提供的解決方案的副本。


這是一個非常天真方法來回答你的問題:

var Nightmare = require('nightmare'); 
var vo = require('vo'); 
var nightmare = Nightmare({ 
    show: true 
}); 

var run = function *() { 
    yield nightmare.goto('http://someInfiniteScrollPage.tld'); 

    var previousHeight, currentHeight=0; 
    while(previousHeight !== currentHeight) { 
    previousHeight = currentHeight; 
    var currentHeight = yield nightmare.evaluate(function() { 
     return document.body.scrollHeight; 
    }); 
    yield nightmare.scrollTo(currentHeight, 0) 
     .wait(3000); 
    } 
    yield nightmare.end(); 
}; 

vo(run)(function(err) { 
    console.dir(err); 
    console.log('done'); 
}); 

這種方法有問題:當你去反對一個網頁,actually is an infinite scroll,上面永遠不會結束。此外,.wait()調用可以被替換爲等待滾動元素計數改變爲可能減少延遲並增加健壯性。不過,這應該足以讓你開始。

+0

非常感謝,這是一個很棒的解決方案。你有沒有任何提示如何檢測出現以前沒有的元素,並觸發下一個塊? – Saibottrenham