-1

I took an example from http://phantomjs.org/page-automation.html because I need to press a button on JS site before scraping.

var page = require('webpage').create();
page.open('http://www.sample.com', function() {
  page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
    page.evaluate(function() {
      $("button").click();
    });
    phantom.exit()
  });
});

But when I run this code, I get this error:

work7.rb:5: `$(' is not allowed as a global variable name
work7.rb:5: syntax error, unexpected end-of-input $("button").click();

Is PhantomJS required properly?

var page = require('webpage').create();
  page.open('http://example.com', function(status) {
});

It doesn't work!

/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require': cannot load such file -- webpage (LoadError)

from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require'
from work7.rb:3:in '

What am I doing wrong?

Community
  • 1
  • 1
  • You seem to interpret Javascript with Ruby! – hallucinations Jun 18 '17 at 08:01
  • All I need open page. Wait a bit because JS need some time to render data. Then click one button and scrape page. THATS ALL. Help me please. – rogersnest Jun 18 '17 at 08:55
  • And I just copy fragment from documentation of PhantomJS but it doesnt work! Something wrong with my system? I forget to require something? – rogersnest Jun 18 '17 at 09:06
  • The code you have above is JavaScript. You need to execute it with a JavaScript run-time like node.js. But you can’t use Ruby to do that. – idmean Jun 18 '17 at 09:22
  • I understand. The main web site about PhantomJS is not for Ruby. For Ruby we should use https://github.com/teampoltergeist/poltergeist/tree/v1.15.0 OR https://readysteadycode.com/howto-scrape-websites-with-ruby-and-poltergeist – rogersnest Jun 18 '17 at 13:29

1 Answers1

0

This question is really confusing...

Your code is written in JavaScript, not in Ruby! To execute JavaScript code, you need a JavaScript runtime environment. The most popular ones nowadays are (graphical and headless) web browsers, Node.js and Electron.

PhantomJS is a web browser. It does not have a graphical interface, but this is a real, full-fledged, web browser that you can automate with scripts. To run a regular PhantomJS script written in JavaScript, you have to use the phantomjs command that should be available in your PATH if you installed phantomjs-prebuilt globally on your system with npm.

To help you getting started, you will find below a working example (without jQuery) where PhantomJS clicks a button of the webpage which triggers a JS alert containing the following message: "It works!". This message is captured by the onAlert event handler and printed to the console.

HTML (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>PhantomJS</title>
</head>
<body>
  <button>OK</button>
  <script>
    (function () {
      var button = document.querySelector('button');

      button.addEventListener('click', function () {
        alert('It works!');
      }, false);
    })();
  </script>
</body>
</html>

JavaScript (script.js)

var page = require('webpage').create();

page.onAlert = function (msg) {
  console.log(msg);
};

page.open('index.html', function () {
  page.evaluate(function () {
    document.querySelector('button').click();
  });

  phantom.exit()
});

Command

phantomjs script.js
Badacadabra
  • 8,043
  • 7
  • 28
  • 49