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();
}
非常感謝,這是一個很棒的解決方案。你有沒有任何提示如何檢測出現以前沒有的元素,並觸發下一個塊? – Saibottrenham