我正在尝试使用Casperjs将此page抓取。我的代码的主要功能运行良好,但是内容是动态加载的,我不知道如何触发它。
这就是我现在正在做的:
casper.waitFor(function() {
this.scrollToBottom();
var count = this.evaluate(function() {
var match = document.querySelectorAll('.loading-msg');
return match.length;
});
if (count <= 1) {
return true;
}
else {
return false
};
}, function() { // do stuff });
即使我将超时时间增加到20秒,等待超时也将过期,并且永远不会加载新内容。我已尝试根据我的情况调整此功能:
function tryAndScroll(casper) {
casper.waitFor(function() {
this.page.scrollPosition = { top: this.page.scrollPosition["top"] + 4000, left: 0 };
return true;
}, function() {
var info = this.getElementInfo('p[loading-spinner="!loading"]');
if (info["visible"] == true) {
this.waitWhileVisible('p[loading-spinner="!loading"]', function () {
this.emit('results.loaded');
}, function () {
this.echo('next results not loaded');
}, 5000);
}
}, function() {
this.echo("Scrolling failed. Sorry.").exit();
}, 500);
}
但是我无法弄清楚,我什至不确定这里是否有意义。
有任何想法吗?
最佳答案
我已经看过页面了。它的行为如此,以至于当您跳到最后时不会加载中间图像。
加载页面时,前几行已完全加载,而另一些还没有完全加载(由'.loading-msg'
元素表示的图像丢失)。当您使用this.scrollToBottom();
跳到最后时,没有连续滚动。它跳到最后,页面JavaScript并没有检测到中间图像在视口中,而是短暂地。该页面将继续加载下一行,但不会加载跳过的行的丢失图像。
您必须减少两个摘录中跳跃的距离。
第一个可以这样更改:
var pos = 0,
height = casper.page.viewportSize.height;
casper.waitFor(function() {
this.scrollTo(0, pos * height);
return !this.exists('.loading-msg');
}, function() { // do stuff }, 20000);
第二个可能通过更改
this.page.scrollPosition = { top: this.page.scrollPosition["top"] + 4000, left: 0 };
至
var height = casper.page.viewportSize.height;
this.page.scrollPosition = { top: this.page.scrollPosition.top + height, left: 0 };