javascript - Lazy load scroll with nightmare JS -
i'm new nightmarejs , wrote script scrape website. works that. login profile, wait site load, go likes profile , there want scroll down till end of site. @ moment use kind of ugly work around , wonder if there way scroll down bottom of page results, , go next step.
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 = 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 element, don't overlaod 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(); }
i think you're after similar this answer, related segmentio/nightmare#625.
for completeness, copy of solution provided in referenced answers included below.
this very naive method answer question:
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'); });
this approach has problems: when you're going against page actually infinite scroll, above never end. also, .wait()
call replaced waiting scroll element count change possibly reduce latency , increase robustness. still, should enough started.
Comments
Post a Comment