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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.