I'm trying to scrape a page that uses infinite scroll using phantomjs casperjs and spooky. It is supposed to continue clicking the more button and taking the new links from the results until its stopped manually. The script however starts using more and more memory until it crashes. I wrote the following script, is there a way to optimise it so it won't use as much memory:
function pressMore(previousLinksLength) {
this.click('#projects > div.container-flex.px2 > div > a');
this.wait(1000, function() {
links = this.evaluate(function() {
var projectPreview = document.querySelectorAll('.project-thumbnail a');
return Array.prototype.map.call(projectPreview, function(e) {
return e.getAttribute('href');
});
});
this.emit('sendScrapedLinks', links.slice(previousLinksLength));
// repeat scrape function
pressMore.call(this, links.length);
});
}
// spookyjs starts here
spooky.start(scrapingUrl);
//press the more button
spooky.then(pressMore);
spooky.run();