Skip to content

Instantly share code, notes, and snippets.

@tobilg
Last active December 22, 2015 15:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobilg/6493767 to your computer and use it in GitHub Desktop.
Save tobilg/6493767 to your computer and use it in GitHub Desktop.
Async usage of multiple PhantomJS instances with NodeJS
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);
});
@wcccode
Copy link

wcccode commented Aug 16, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment