admin管理员组文章数量:1420086
This question is similar to others, but the problem I had was more basic.
This is my code:
var links = [];
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('div#mw-content-text table.wikitable tbody tr td i b a');
return Array.prototype.map.call(links, function(e) {
return '' + e.getAttribute('href');
});
}
casper.start('');
casper.then(function() {
// aggregate results for the 'casperjs' search
links = this.evaluate(getLinks);
});
casper.each(links, function (self, link) {
self.thenOpen(fullURL, function () {
this.echo(this.getTitle() + " - " + link);
});
});
casper.run();
I know that links
get created as it is copied from the Quickstart, but I then modified it to open all the links that were found.
What I'm getting is that nothing is echo'd instead of outputting the each title which is what I expect. This is how I'm calling the file:
~ $ casperjs casper-google-disco.js
This question is similar to others, but the problem I had was more basic.
This is my code:
var links = [];
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('div#mw-content-text table.wikitable tbody tr td i b a');
return Array.prototype.map.call(links, function(e) {
return 'https://en.wikipedia' + e.getAttribute('href');
});
}
casper.start('https://en.wikipedia/wiki/David_Bowie_discography');
casper.then(function() {
// aggregate results for the 'casperjs' search
links = this.evaluate(getLinks);
});
casper.each(links, function (self, link) {
self.thenOpen(fullURL, function () {
this.echo(this.getTitle() + " - " + link);
});
});
casper.run();
I know that links
get created as it is copied from the Quickstart, but I then modified it to open all the links that were found.
What I'm getting is that nothing is echo'd instead of outputting the each title which is what I expect. This is how I'm calling the file:
~ $ casperjs casper-google-disco.js
Share
Improve this question
edited May 23, 2017 at 12:17
CommunityBot
11 silver badge
asked May 16, 2016 at 9:14
icc97icc97
12.9k9 gold badges83 silver badges97 bronze badges
1 Answer
Reset to default 8The fix was very easy in the end, but took me ages to find it as there were no errors and no-one else seemed to have hit this.
The problem is that the links
variable doesn't get set before the each
is called. Putting the each
inside the then
function solves my problem.
The each.js
example in the CasperJS samples was helpful for confirming that you can loop through an array without any need for IIFE.
var links = [];
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('div#mw-content-text table.wikitable tbody tr td i b a');
return Array.prototype.map.call(links, function(e) {
return 'https://en.wikipedia' + e.getAttribute('href');
});
}
casper.start('https://en.wikipedia/wiki/David_Bowie_discography');
casper.then(function() {
// aggregate results for the 'casperjs' search
links = this.evaluate(getLinks);
casper.each(links, function (self, link) {
self.thenOpen(link, function () {
this.echo(this.getTitle() + " - " + link);
});
});
});
casper.run();
本文标签: javascriptCasperJS looping through each URLStack Overflow
版权声明:本文标题:javascript - CasperJS looping through each URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745328146a2653686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论