-2

I downloaded many pages html with casperjs, but I want separate them in 2 folders. some files contains id="trie" and others not.

What can I do, please?

this is my script

casper.repeat(j , function(){
  casper.open(arrdata[i]); // arrdata[i] : contains url of file to download
  casper.wait(5000, function(){
    file = 'pages/road/r_' + i +'.html';
    fs.write(file, casper.getHTML(), 'w');
  });
  i++;
});

but i want do like this

casper.repeat(j, function(){
  casper.open(arrdata[i]);
  if ("id=trie" exists in contains of arrdata[i] ){
    this file is put to pages/trie/...html
  } else {
    this file is put to pages/road/...html
  }
  i++;
});
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
lalao
  • 11
  • 4

1 Answers1

0

You want to use the exists function:

var i = 0;
var fs = require('fs');
casper.repeat(j, function(){
  (function(i){
    casper.thenOpen(arrdata[i], function(){
      if (casper.exists("#Trie")){
        fs.write("pages/trie/"+i+".html", casper.getHTML());
      } else {
        fs.write("pages/road/"+i+".html", casper.getHTML());
      }
    });
  })(i);
  i++;
});

Note that this must be done in a step, otherwise you run into concurrency problems. In your code all the open calls are executed before the first wait is executed.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • thank you very much, I'll test this and I will keep you posted – lalao Nov 27 '14 at 12:19
  • what you mean in a step please? the files are all in pages/road/ ;( – lalao Nov 27 '14 at 12:30
  • sorry forgot the IIFE. Are you sure that there are pages which contain `id="trie"`? – Artjom B. Nov 27 '14 at 12:32
  • sorry, it's with Trie but not trie, it's my error, but what is ||FE, please? – lalao Nov 27 '14 at 15:10
  • [This](http://stackoverflow.com/a/19324832) shows pretty good what it is and why it is necessary. – Artjom B. Nov 27 '14 at 15:13
  • I am debutante on the subject, I do not understand , you can explain to me again? – lalao Nov 27 '14 at 15:22
  • ||FE is for having 1,2,3 .... in the folder pages/trie and in the pages/road ? but not 1,4,15 ... in the first and 2,5,... in the second, or I'm wrong? – lalao Nov 27 '14 at 15:28
  • I did it already. It is the `(function(i){ /* inner code */ })(i)`. You can look at the [second revision](http://stackoverflow.com/posts/27170523/revisions). It has to be done, because `thenOpen` is asynchronous so the `i` inside will always refer to the last value that `i` is taking. Without the IIFE you would only receive one file in each of the folders. Just try it out and take out the IIFE, but delete every file in those folders. The files will be overwritten `i-1` times. – Artjom B. Nov 27 '14 at 15:33
  • I'm sorry , i haven't seen. Thanks a lot!! – lalao Nov 27 '14 at 15:37
  • I want have a file having noun as 1.html, 2.html , 3.html and so on for each folder, please. – lalao Nov 27 '14 at 15:53
  • Of course it is possible, but I'm not here to just give you code. Try it yourself. – Artjom B. Nov 27 '14 at 16:01