D
D
dhat2016-10-02 16:43:11
JavaScript
dhat, 2016-10-02 16:43:11

Is PhantomJS a Bad Choice?

Started to deal with PhantomJS. I am running a simple script:

var system = require('system');
var os = system.os;
var args = system.args;

var scriptName = args[0];
var name = args[1];
var url = args[2];

var page = require('webpage').create();
// ERROR HANDLING
var webPage = require('webpage');
var page = webPage.create();

page.onError = function(msg, trace) {

  var msgStack = ['ERROR: ' + msg];

  if (trace && trace.length) {
    msgStack.push('TRACE:');
    trace.forEach(function(t) {
      msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
    });
  }

  console.error(msgStack.join('\n'));

};
// END

if(args.length === 2) {
  throw new Error('Name as system argument missing.');
}


console.log(
  'Hey ' + name + '.\n' +
  'You PC is ' + os.architecture + ' ' + os.name + os.version + '.\n' +
  'Executed: ' + scriptName + '.\n' +
  'Opening ' + url + '...'
);

page.open(url, function(status) {
  console.log('Status: ' + status);
  // Do other things here...

  page.viewportSize = {
    width: 1200,
    height: 1000
  };
  page.render('image1200.png');
  console.log('Rendered image: ' + url + '(1200px)');

  page.viewportSize = {
    width: 800,
    height: 1000
  };
  page.render('image800.png');
  console.log('Rendered image: ' + url + '(800px)');

  page.viewportSize = {
    width: 500,
    height: 1000
  };
  page.render('image500.png');
  console.log('Rendered image: ' + url + '(500px)');

  // EXIT
  phantom.exit();
});

Which just goes to the site and takes 3 screenshots with different viewports. Elementary like? So, I go to bloomberg.com - and I get a 100% load of the processor, the script runs for 3 minutes (lol!), As a result, the screenshots are CURVE.
Total - to send to the trash, or further sorted out? My goal is to find a tool for automating some actions on sites (for example, registration, posting on social networks, etc.), as well as web scraping (the same pages on social networks, as well as various html tables).
Are there better alternatives for my purposes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Sundukov, 2016-11-12
@dhat

PhantonJS is not perfect, of course, but as a scripter it works quite well for itself. 3 minutes is certainly overkill, the problem is clearly somewhere in the written JS code. I made 3 screenshots ( 1200*1000 , 800*1000 , 500*1000 ) of the given page, it turned out somewhere in ~ 7 seconds for each (while PhantomJS is on a remote machine and goes through a proxy itself). The page itself, yes, swam a little. But it is possible, because it is based on an old webkit. If you need screenshots from the latest versions of browsers, then of course it may not be suitable.

F
fsockopen, 2016-10-02
@fsockopen

maybe because of this?

page.viewportSize = {
    width: 1200,
    height: 1000
  };
  page.render('image1200.png');
  console.log('Rendered image: ' + url + '(1200px)');

  page.viewportSize = {
    width: 800,
    height: 1000
  };
  page.render('image800.png');
  console.log('Rendered image: ' + url + '(800px)');

  page.viewportSize = {
    width: 500,
    height: 1000
  };
  page.render('image500.png');
  console.log('Rendered image: ' + url + '(500px)');

may not have time to process and move to another line,
I think that you need to hang the handler. For example, if the first screenshot was successfully preserved, do the second then the third ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question