【发布时间】:2014-09-07 15:06:05
【问题描述】:
我正在尝试使用 phantomjs casperjs 和 spooky 抓取使用无限滚动的页面。它应该继续单击更多按钮并从结果中获取新链接,直到手动停止。然而,该脚本开始使用越来越多的内存,直到它崩溃。我写了以下脚本,有没有办法优化它,所以它不会占用太多内存:
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();
【问题讨论】:
-
sendScrapedLinks事件处理程序有多复杂?除此之外,你不能做得更好。 -
这只是一个简单的 mongoose.save() 函数。
标签: node.js web-scraping phantomjs casperjs spookyjs