问题描述
我试图点击下一个按钮N次并每次抓取页面源。我知道我可以在远程网站上运行任意函数,所以我只使用远程函数nextPage()而不是click()我如何运行以下任意次数:
I am trying to click a 'next' button N number of times and grab the page source each time. I understand that I can run an arbitrary function on the remote website, so instead of click() I just use the remote function nextPage() How do I run the following, an arbitrary number of times:
var casper = require('casper').create();
casper.start('http://www.example.com', function() {
this.echo(this.getHTML());
this.echo('-------------------------');
var numTimes = 4, count = 2;
casper.repeat(numTimes, function() {
this.thenEvaluate(function() {
nextPage(++count);
});
this.then(function() {
this.echo(this.getHTML());
this.echo('-------------------------');
});
});
});
'我'这是我尝试在javascript中使用的索引for循环。
'i' here is an index I tried to use in a javascript for loop.
所以tl;博士:我想舔'下一步',打印页面来源,点击'下一步',打印页面来源,点击'下一步'...继续N次。
So tl;dr: I want lick 'next', print pages source, click 'next', print page source, click 'next'... continue that N number of times.
推荐答案
首先,您可以将值传递给远程页面上下文(即 thenEvaluate
这样的函数:
First, you can pass a value to the remote page context (i.e. to thenEvaluate
function like this:
this.thenEvaluate(function(remoteCount) {
nextPage(remoteCount);
}, ++count);
然而, Casper #repact
可能不是一个好用的函数,因为循环不会等待每个页面加载然后捕获内容。
However, Casper#repeat
might not be a good function to use here as the loop would NOT wait for each page load and then capture the content.
你可能宁愿设计一个基于事件的链接。
You may rather devise a event based chaining.
代码的工作流程是:
-
将一个全局变量(或者至少是下面提到的函数可访问的变量)存储到stor e
计数
和限制
。
收听事件并在此处抓取HTML,然后调用下一页。
listen to the load.finished
event and grab the HTML here and then call the next page.
A简化代码可以是:
var casper = require('casper').create();
var limit = 5, count = 1;
casper.on('load.finished', function (status) {
if (status !== 'success') {
this.echo ("Failed to load page.");
}
else {
this.echo(this.getHTML());
this.echo('-------------------------');
}
if(++count > limit) {
this.echo ("Finished!");
}
else {
this.evaluate(function(remoteCount) {
nextPage(remoteCount);
// [Edit the line below was added later]
console.log(remoteCount);
return remoteCount;
}, count);
}
});
casper.start('http://www.example.com').run();
this.wait(
1000, // in ms
function () {
this.evaluate(function(remoteCount) {
nextPage(remoteCount);
}, count);
}
);
// help is tracing page's console.log
casper.on('remote.message', function(msg) {
console.log('[Remote Page] ' + msg);
});
// Print out all the error messages from the web page
casper.on("page.error", function(msg, trace) {
casper.echo("[Remote Page Error] " + msg, "ERROR");
casper.echo("[Remote Error trace] " + JSON.stringify(trace, undefined, 4));
});
这篇关于如何在casperjs中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!