javascript - probleme with while in PhantomJS -


i want make script allow me phantomjs automatically visit web pages want, source code , saves moves next link. visit link , save source code, problem loop not working visits link , in visit.

i grateful assistance provided, internship project must return quickly!

here code:

var nombresaison = 8; var nombreepisode = 9; var saisonactuel = 1; var episodeactuel = 1;  while(nombreepisode < episodeactuel) {     function waitfor(testfx, onready, timeoutmillis) {         var maxtimeoutmillis = timeoutmillis ? timeoutmillis : 3000, //< default max timout 3s             start = new date().gettime(),             condition = false,             interval = setinterval(function() {                 if ( (new date().gettime() - start < maxtimeoutmillis) && !condition ) {                     // if not time-out yet , condition not yet fulfilled                     condition = (typeof(testfx) === "string" ? eval(testfx) : testfx()); //< defensive code                 } else {                     if(!condition) {                         // if condition still not fulfilled (timeout condition 'false')                         console.log("'waitfor()' timeout");                         phantom.exit(1);                     } else {                         // condition fulfilled (timeout and/or condition 'true')                         console.log("'waitfor()' finished in " + (new date().gettime() - start) + "ms.");                         typeof(onready) === "string" ? eval(onready) : onready(); //< it's supposed once condition fulfilled                         clearinterval(interval); //< stop interval                     }                 }             }, 250); //< repeat check every 250ms     };      var page = require('webpage').create();     page.open("http://www.dpstream.net/serie-5907-penny-dreadful-saison-1-episode-01-fr.html", function (status)      {         // check page load success         if (status !== "success")          {             console.log("unable access network");         }          else          {             // wait 'signin-dropdown' visible             waitfor(function()              {                 // check in page if specific element visible                 return page.evaluate(function()                  {                     return $(".piedpage").is(":visible");                 });             }, function()              {                 var fs = require('fs');                 var path = 'c:\\phantomjs\\fichier\\episode_1.html';                 var js = page.evaluate(function () { return document; });                 fs.write(path, js.all[0].outerhtml, 'w');                 console.log("succes.");                 episodeactuel++;                 phantom.exit();             });                 }     }); } 

thanks !

phantomjs's page.open() asynchronous. means call it, next statement executed. means whole while loop iterated through before page began loading first page. in fact, last page loaded, because calling page.open() second time discard first invocation in process.

there 2 ways solve this:

  • create new page every invocation (not recommended)
  • use recursion: define function thing single iteration , call next iteration after you've done iteration. if there no more iterations, can call phantom.exit().

be sure out not exiting prematurely after first iteration.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -