Last active
December 22, 2015 15:39
-
-
Save tobilg/6493767 to your computer and use it in GitHub Desktop.
Async usage of multiple PhantomJS instances with NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var phantom = require('phantom'); | |
var async = require('async'); | |
var pagesToCall = [ | |
['http://www.google.com', 8000], | |
['http://www.allthingsd.com', 8001], | |
['http://www.wired.com', 8002], | |
['http://www.mashable.com', 8003], | |
['http://www.stackoverflow.com', 8004] | |
]; | |
function callPage(pageToCall) { | |
console.log(new Date().getTime() + ': Started page ' + pageToCall[0]); | |
console.log(new Date().getTime() + ': port:' + pageToCall[1]); | |
phantom.create({port: pageToCall[1]}, function(ph) { | |
ph.createPage(function(page) { | |
page.open(pageToCall[0], function(status) { | |
console.log(new Date().getTime() + ': Opened page? %s', status); | |
page.set('viewportSize', { | |
width: 1280, | |
height: 800 | |
}); | |
page.set('settings.userAgent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1'); | |
var filename = pageToCall[0].replace('http:\/\/', '') + '.png'; | |
page.render('./screenshots/' + filename, function() { | |
console.log(new Date().getTime() + ': Wrote page ' + pageToCall[0]); | |
page.close(); | |
ph.exit(); | |
}); | |
}); | |
}); | |
}); | |
} | |
//Main | |
async.each(pagesToCall, callPage, function (e) { | |
if (e) console.log(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Multiple PhantomJSw with node, Every phantomjs will send http request . Memory and bandwidth is a bottleneck, how do you optimize. What is the biggest numbers of child phantomjs process.