在PhantomJS中遇到问题

时间:2015-07-14 08:51:55

标签: javascript while-loop phantomjs

我想创建一个脚本,允许我PhantomJS自动访问我想要的网页,它的源代码并保存它移动到下一个链接。 现在我可以访问该链接并保存源代码,但问题是我的循环无法正常工作,她访问了一个链接,有时它也可以访问。

我很感激所提供的帮助,这是一个实习项目,我必须快速回来!

这是我的代码:

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 is 3s
            start = new Date().getTime(),
            condition = false,
            interval = setInterval(function() {
                if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
                    // If not time-out yet and condition not yet fulfilled
                    condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
                } else {
                    if(!condition) {
                        // If condition still not fulfilled (timeout but condition is 'false')
                        console.log("'waitFor()' timeout");
                        phantom.exit(1);
                    } else {
                        // Condition fulfilled (timeout and/or condition is 'true')
                        console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                        typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                        clearInterval(interval); //< Stop this 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 for page load success
        if (status !== "success") 
        {
            console.log("Unable to access network");
        } 
        else 
        {
            // Wait for 'signin-dropdown' to be visible
            waitFor(function() 
            {
                // Check in the page if a specific element is now 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();
            });        
        }
    });
}

谢谢!

1 个答案:

答案 0 :(得分:0)

PhantomJS的page.open()是异步的。这意味着只要调用它,就会执行下一个语句。这意味着在page甚至开始加载第一页之前迭代整个while循环。实际上,只会加载最后一页,因为第二次调用page.open()将丢弃正在进行的第一次调用。

有两种方法可以解决这个问题:

  • 为每次调用(不推荐)
  • 创建新的page
  • 使用递归:定义一个函数,它可以完成一次迭代,并在完成此迭代后调用下一次迭代。如果没有更多迭代,您最终可以调用phantom.exit()

请务必注意不要像第一次迭代后那样过早退出。