用于在弹出窗口内进行分页和网页抓取的 CasperJS 步骤的动态数量
Dynamic number of CasperJS steps for paginating and web scraping inside a popup
正在尝试抓取大量 JS 的分页列表。
我可以看到总页数,因此我知道我需要 运行 .waitForSelectorTextChange()
多少次。这不起作用:
casper.withPopup(/NewInventoryManager/, function() {
var target = 3;
for (var i = 0; i < target; i++) {
this.click('#btnNext'); // Click next and initiate a loading.gif modal
casper.waitForSelectorTextChange('#some_div', function() {
this.echo('Here I can parse the now updated DOM.');
}, function() {}, 35000);
}
});
它 运行 一次通过脚本并一次设置所有 .waitForSelectorTextChange
功能,当初始点击导致 DOM 改变时。
如何以编程方式在脚本中添加动态步数?它似乎在工作,某种程度上。我怎样才能让他们一个接一个地走?
受到 by this answer 的启发,这里是递归在 .withPopup
中的样子(实际上是一样的):
function scrapeAndClick() {
// Total pages count
target = this.evaluate(function addID() { return $('#pagecountspan').text(); });
target = parseInt(target.replace(/[^0-9.]/g, ''));
// Current page (usually 1)
var current = casper.evaluate(function getCurrent() { return parseInt($('#ddlpaging').val()); });
casper.waitForSelectorTextChange('#moduleInventoryBrowserWrapper', function() {
this.echo('The text on #moduleInventoryBrowserWrapper has been changed :: Scrape now!');
// Now + 1
current = casper.evaluate(function getCurrent() { return parseInt($('#ddlpaging').val()); });
if (current < target) {
casper.then(scrapeAndClick);
} else {
casper.echo('Finished!');
}
}, function onTm() { this.echo('timeout 1'); }, 35000);
}
casper.withPopup(/NewInventoryManager/, function() {
this.echo('Popup detected: ' + this.getTitle());
casper.then(scrapeAndClick);
});
正在尝试抓取大量 JS 的分页列表。
我可以看到总页数,因此我知道我需要 运行 .waitForSelectorTextChange()
多少次。这不起作用:
casper.withPopup(/NewInventoryManager/, function() {
var target = 3;
for (var i = 0; i < target; i++) {
this.click('#btnNext'); // Click next and initiate a loading.gif modal
casper.waitForSelectorTextChange('#some_div', function() {
this.echo('Here I can parse the now updated DOM.');
}, function() {}, 35000);
}
});
它 运行 一次通过脚本并一次设置所有 .waitForSelectorTextChange
功能,当初始点击导致 DOM 改变时。
如何以编程方式在脚本中添加动态步数?它似乎在工作,某种程度上。我怎样才能让他们一个接一个地走?
受到 by this answer 的启发,这里是递归在 .withPopup
中的样子(实际上是一样的):
function scrapeAndClick() {
// Total pages count
target = this.evaluate(function addID() { return $('#pagecountspan').text(); });
target = parseInt(target.replace(/[^0-9.]/g, ''));
// Current page (usually 1)
var current = casper.evaluate(function getCurrent() { return parseInt($('#ddlpaging').val()); });
casper.waitForSelectorTextChange('#moduleInventoryBrowserWrapper', function() {
this.echo('The text on #moduleInventoryBrowserWrapper has been changed :: Scrape now!');
// Now + 1
current = casper.evaluate(function getCurrent() { return parseInt($('#ddlpaging').val()); });
if (current < target) {
casper.then(scrapeAndClick);
} else {
casper.echo('Finished!');
}
}, function onTm() { this.echo('timeout 1'); }, 35000);
}
casper.withPopup(/NewInventoryManager/, function() {
this.echo('Popup detected: ' + this.getTitle());
casper.then(scrapeAndClick);
});