How to make a site-saving queue with for() ?

14 views
Skip to first unread message

martin....@gmail.com

unread,
Jun 29, 2017, 4:24:33 PM6/29/17
to CasperJS
I wonder why I can't get this CasperJS code to work. My main goal is to save some pages, the pages html contains two dynamic values put into url. The problem is that CasperJS seems to run it's code after the for() code is done and only the last page is saved. How can I make this to work? Does CasperJS allow this automatization?


var site_list = new Array('storarge1','storage2','storage3');
var input_list = new Array('y1','y2','y3');


var start_url = 'https://page.com';

var casper = require('casper').create();

casper.start(start_url, function() {
});


//
var time = 3000;

for (var i=0; i<site_list.length; i++) {
for (var j=0; j<input_list.length; j++) {

var site = site_list[i];
var input = input_list[j];

var url = 'https://page.com/' + site + '?p='+ input;
var save = '/mydrive/' + site + '_' + input +'.html';

casper.thenOpen(url, function() {
this.echo(this.getCurrentUrl());

var fs = require('fs');
var html = this.getPageContent();
var f = fs.open(save, 'w');
f.write(html);
f.close();

this.echo(site + ' - ' + input + ' (' + j + '/' + input_list.length +')');

});
}
}

casper.run(function () {
this.echo('Done').exit();
});

The output get to:

https://page.com/storage1?p=y1
storage3 - y3 (9 / 9)
https://page.com/storage1?p=y2
storage3 - y3 (9 / 9)
https://page.com/storage1?p=y3
storage3 - y3 (9 / 9)
https://page.com/storage2?p=y1
storage3 - y3 (9 / 9)
https://page.com/storage2?p=y2
storage3 - y3 (9 / 9)
https://page.com/storage2?p=y3
storage3 - y3 (9 / 9)
https://page.com/storage3?p=y1
storage3 - y3 (9 / 9)
https://page.com/storage3?p=y2
storage3 - y3 (9 / 9)
https://page.com/storage3?p=y3
storage3 - y3 (9 / 9)

So it goes through all 9 urls and saves them all with the last sites values instead of making 12 unique saved files.

In my folder only one file is there at the end:
storage3_y3.html

Help please!

Ken

unread,
Jun 30, 2017, 7:14:45 AM6/30/17
to CasperJS
Can you try if for the immediate { after for loop, replace it with {casper.then(function() { and then close the loop with })}


I'm able to work with for loops this way in CasperJS for a web automation tool I make base on CasperJS (https://github.com/tebelorg/TagUI). However, while loops still do not work well and I think the async/wait nature somehow gets broken (when you use casper.then it will hang the while loop).

Reply all
Reply to author
Forward
0 new messages