【发布时间】:2015-03-13 22:59:37
【问题描述】:
我目前正在使用 PhantomJS 和 CasperJS 来抓取网站中的链接。该站点使用 javascript 来动态加载结果。然而,下面的 sn-p 并没有让我得到页面包含的所有结果。我需要向下滚动到页面底部,查看微调器是否出现(意味着还有更多内容要来),等到新内容加载完毕,然后继续滚动直到不再显示新内容。然后将类名.title 的链接存储在一个数组中。链接到webpage 进行抓取。
var casper = require('casper').create();
var urls = [];
function tryAndScroll(casper) {
casper.waitFor(function() {
this.page.scrollPosition = { top: this.page.scrollPosition["top"] + 4000, left: 0 };
return true;
}, function() {
var info = this.getElementInfo('.badge-post-grid-load-more');
if (info["visible"] == true) {
this.waitWhileVisible('.badge-post-grid-load-more', function () {
this.emit('results.loaded');
}, function () {
this.echo('next results not loaded');
}, 5000);
}
}, function() {
this.echo("Scrolling failed. Sorry.").exit();
}, 500);
}
casper.on('results.loaded', function () {
tryAndScroll(this);
});
casper.start('http://example.com/', function() {
this.waitUntilVisible('.title', function() {
tryAndScroll(this);
});
});
casper.then(function() {
casper.each(this.getElementsInfo('.title'), function(casper, element, j) {
var url = element["attributes"]["href"];
urls.push(url);
});
});
casper.run(function() {
this.echo(urls.length + ' links found:');
this.echo(urls.join('\n')).exit();
});
【问题讨论】:
标签: javascript phantomjs casperjs