gpt4 book ai didi

javascript - CasperJS 循环无法正确评估,显然只是直接运行

转载 作者:行者123 更新时间:2023-12-03 12:00:55 26 4
gpt4 key购买 nike

我有以下代码:

        function getLinks() {
var links = document.querySelectorAll('a.row-link');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href');
});
}


casper.start('http://somedomain.com/board/search/search/?p=' + pagee);

// Get all the pages here!
casper.then(function(){
while(pagenos = nextpagereg.exec(this.getHTML())) {;
this.echo(pagenos);
pages.push(pagenos);


}
this.echo(pages.length + ' PAGES FOUND');
return pages;
});


casper.then(function() {
pagee = 0;

links = this.evaluate(getLinks);
this.echo(links.length + ' links found:');


this.each(pages, function(self,page){
pagee++;
this.echo('WORKING ON PAGE' + pagee);
self.thenOpen('http://somedomain.com/board/search/search/?p=' + pagee, function(self){
this.each(links, function(self, link){

self.thenOpen(link, function(self, a){

title = this.getHTML('h2#job-title').trim();
if(casper.exists('p#job-subtitle a')){
company = this.getHTML('p#job-subtitle a');
} else {
title = "NA";
}

loc = this.getHTML('p#job-subtitle>strong');
email = regex.exec(this.getHTML());
this.echo("Title : " + title);
this.echo("Company : " + company);
this.echo("Location : " + loc);
this.echo("Email : " + email);


this.echo("************************************************************************************************************");


});
//this.echo(link);

});

});
});

});

casper.run(function() {
this.exit();
});

问题似乎是它没有按顺序运行页面,外部循环只是崩溃了所有页面链接!

编辑

澄清一下这是输出,因为您可以看到循环在处理每个页面之前遍历所有页面增量:

    http://somedomain.com/job-board/search/@/?p=2
http://somedomain.com/job-board/search/search/?p=3
http://somedomain.com/job-board/search/search/?p=4
http://somedomain.com/job-board/search/search/?p=5
http://somedomain.com/job-board/search/search/?p=6
5 PAGES FOUND
50 links found in page 1:
LOOP START
WORKING ON PAGE1
WORKING ON PAGE2
WORKING ON PAGE3
WORKING ON PAGE4
WORKING ON PAGE5

最佳答案

看起来您需要抓取五个页面中每一页上的链接,但在开始迭代之前只需执行一次。

你应该添加

links = this.evaluate(getLinks);
this.echo(links.length + ' links found:');

进入 self.thenOpen('http://somedomain.com/board/search/search/?p=' + pagee, ...each 之前.

更简洁的方法是将getLinks移动self.thenOpen并交换最顶层

this.each(pages, function(self,page){
...
});

for(var page = 0; page < pages.length; page++){
...
}

关于javascript - CasperJS 循环无法正确评估,显然只是直接运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25421629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com