通过使用JavaScript phantomjs URL的数组循环 [英] loop through array of urls in phantomjs using javascript

查看:311
本文介绍了通过使用JavaScript phantomjs URL的数组循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的code遍历URL的数组,但被卡住。

code的该位只是运行在phantomjs并输出请求的URL和任何主要的资源对象的重定向。

我想用一个数组作为输入到这样的过程:

  VAR PAGEURL = [
http://www.google.com,
http://www.facebook.com
];

这里的原

  VAR SYS =要求('系统');
VAR PAGEURL ='http://www.google.com';
的console.log(请求的URL:+ PAGEURL);VAR renderPage =功能(URL){
    VAR页=要求('网页')创建()。    page.onNavigationRequested =功能(URL,类型,willNavigate,主){        如果(主,放大器;&安培;!URL = PAGEURL){
            的console.log(重定向URL:+网址)
        }
    };    page.open(URL,功能(状态){
            如果(状态!=='成功'){
                phantom.exit(1);
            }其他{
                的setTimeout(函数(){
                    phantom.exit(0);
                },0);
            }
        });
};
renderPage(PAGEURL);


解决方案

  VAR的URL = [
http://www.google.com,
http://www.facebook.com
];
功能处理(){
    如果(urls.length == 0){
        phantom.exit();
    }其他{
        //删除数组的第一个项目
        URL = urls.shift();
        //打开一个页面
        页=要求('网页')创建()。        //请求的URL存储在一个单独的变量
        VAR CURRENTURL =网址
        page.open(URL,onFinishedLoading)        page.onNavigationRequested =功能(URL,类型,willNavigate,主){
            的console.log('\\ n'+ CURRENTURL +\\ nredirecting为\\ n+网址);
        }    }
}功能onFinishedLoading(状态){    的console.log(状态);
    page.release();
    处理();
}处理();

I'm trying to get my code to loop through an array of urls but getting stuck.

This bit of code simply runs in phantomjs and outputs the requested url and the redirects for any main resource objects.

I'd like to use an array as an input to this process like:

var pageUrl = [
'http://www.google.com',
'http://www.facebook.com'
];

here's the original

var sys = require('system');
var pageUrl = 'http://www.google.com';


console.log("Requested URL: " + pageUrl);



var renderPage = function (url) {
    var page = require('webpage').create();

    page.onNavigationRequested = function(url, type, willNavigate, main) {

        if (main && url!=pageUrl) {
            console.log("Redirected URL: " + url)
        }
    };



    page.open(url, function(status) {
            if ( status !== 'success' ) {
                phantom.exit(1);
            } else {
                setTimeout(function() {
                    phantom.exit(0);
                }, 0);
            }
        });
};


renderPage(pageUrl);

解决方案

var urls = [
'http://www.google.com',
'http://www.facebook.com'
];


function process() {
    if (urls.length == 0) {
        phantom.exit();
    } else {
        //remove the first item of an array
        url = urls.shift();
        //open a page
        page = require('webpage').create();

        //store the requested url in a separate variable
        var currentUrl = url


        page.open(url, onFinishedLoading)

        page.onNavigationRequested = function(url, type, willNavigate, main) {
            console.log('\n' + currentUrl + '\nredirecting to \n' + url);
        }

    }
}

function onFinishedLoading(status) {

    console.log(status);
    page.release();
    process();
}

process();

这篇关于通过使用JavaScript phantomjs URL的数组循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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