我是 NightmareJS 的新手,写了一个脚本来抓取一个网站。这就像那样工作。我登录到我的个人资料,等待网站加载,然后转到我喜欢的个人资料,然后我想向下滚动到网站末尾。目前我使用这种丑陋的工作,想知道是否有一种方法可以向下滚动到页面底部以获取所有结果,然后进行下一步。
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();
}