使用PhantomJS评估错误访问多个URL [英] Visiting multiple urls using PhantomJS evaluating Error

查看:75
本文介绍了使用PhantomJS评估错误访问多个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个漂亮的代码,所有我想在两次访问之间进行一些暂停,所以我添加了一个'setinterval',但这不起作用:

I have this beautiful code, all I want to make some pause between visits, so I add a 'setinterval', but this not works:

var page = require('webpage').create();

// the urls to navigate to
var urls = [
    'http://blogger.com/',
    'https://github.com/',
    'http://reddit.com/'
                ];
    var i = 0;

// the recursion function
var genericCallback = setInterval(function () {
    return function (status) {
        console.log("URL: " + urls[i]);
        console.log("Status: " + status);
        // exit if there was a problem with the navigation
        if (!status || status === 'fail') phantom.exit();

        i++;

        if (status === "success") {

            //-- YOUR STUFF HERE ---------------------- 
            // do your stuff here... I'm taking a picture of the page
            page.render('example' + i + '.png');
            //-----------------------------------------

            if (i < urls.length) {

                // navigate to the next url and the callback is this function (recursion)
                page.open(urls[i], genericCallback());

            } else {
                // try navigate to the next url (it is undefined because it is the last element) so the callback is exit
                page.open(urls[i], function () {
                    phantom.exit();
                });
            }
        }
    };
},2000);

// start from the first url
page.open(urls[i], genericCallback());

出现错误的屏幕截图: 也许有人可以帮助我并治愈此代码?因为我是JS和PhantomJS的新手,所以对您的帮助将不胜枚举. 我从另一个stackoverflow答案这里获得了此代码-在单个脚本中使用多个page.open 但我无法向作者发表评论,因为我没有50个声誉

the screenshot with the error I get: maybe someone could help me and heal this code? Because I'm new to JS and to PhantomJS, any help will be apreciate. I got this code from another stackoverflow answer here - Using Multiple page.open in Single Script but I can't comment to the author , because I don't have 50 reputation

推荐答案

它应该像这样:

var page = require('webpage').create();

var urls = ['http://blogger.com/','https://github.com/','http://reddit.com/'];
var i = 0;

function OpenPage(){
    setTimeout(function(){
        page.open(urls[i], function(status) {
            if (status == 'success') {
                    page.render('example' + i + '.png');
            }
            i++;
            if(i <= urls.length - 1){ 
                OpenPage();
            }else{
               phantom.exit();
            }
        });
    },2000);
}

OpenPage();

这篇关于使用PhantomJS评估错误访问多个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆