最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Phantomjs - ReferenceError: Can't find variable: $ - Stack Overflow

programmeradmin1浏览0评论

I have a PhantomJS script that works when I run it locally (Mac), but when I run it on my Linux server, it returns the following error:

ReferenceError: Can't find variable: $
:5712 in global code

The code is:

var page = require('webpage').create();
var fs = require('fs');
var args = require('system').args;
page.settings.userAgent = 'SpecialAgent';

page.open('', function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        var ua = page.evaluate(function () {
        var result ="";
        // ...
        return result;
    });    

    }
    phantom.exit();
});

I have a PhantomJS script that works when I run it locally (Mac), but when I run it on my Linux server, it returns the following error:

ReferenceError: Can't find variable: $
https://fantasy.premierleague./a/statistics/value_form:5712 in global code

The code is:

var page = require('webpage').create();
var fs = require('fs');
var args = require('system').args;
page.settings.userAgent = 'SpecialAgent';

page.open('https://fantasy.premierleague./a/statistics/value_form', function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        var ua = page.evaluate(function () {
        var result ="";
        // ...
        return result;
    });    

    }
    phantom.exit();
});
Share Improve this question asked Sep 27, 2016 at 2:03 p_mcpp_mcp 2,8018 gold badges40 silver badges76 bronze badges 5
  • 2 why not just do request to this url: fantasy.premierleague./drf/bootstrap-static ? I don't see the benefit of using phantomjs, cuz in real it automatically requests api and gets the data. – num8er Commented Sep 27, 2016 at 2:11
  • 1 Wow... I cant believe that actually exists, thanks! – p_mcp Commented Sep 27, 2016 at 2:14
  • 1 Which PhantomJS versions do you use? Please register to the onConsoleMessage, onError, onResourceError, onResourceTimeout events (Example). Maybe there are errors. – Artjom B. Commented Sep 27, 2016 at 18:21
  • Using 2.1.1. When I register to those error messages, I get the following: Unable to load resource (#2 URL:ismdj.scdn5.secure.raxcdn./static/CACHE/css/f021cc36944b.css) ... Error code: 6. Description: SSL handshake failed ERROR: ReferenceError: Can't find variable: $ TRACE: -> fantasy.premierleague./a/statistics/value_form: 5712 (in function "global code") Perhaps its an SSL issue? – p_mcp Commented Sep 29, 2016 at 1:02
  • I am getting the same errors as described in the original problem as well as with the registered events per @ArtjomB above. Any further thoughts on resolving this? – Vishal Commented Aug 3, 2017 at 16:49
Add a ment  | 

3 Answers 3

Reset to default 2

There may be a race condition between your code and jQuery being loaded on the page. Wrap the statements in your page.evaluate callback with a $(document).ready(function() { /* your statements here */ }); to ensure scripts on the page have loaded fully.

For anyone who is still using PhantomJS and encounters this problem, I solved it with

phantomjs --ignore-ssl-errors=yes

I don't intend for upvote. I'm providing solution to solve some situations without simulating browser behavior with phantomjs just to retrieve data that can be handled directly by requesting to url.

You need the data from the page, so why not just do request to this url: https://fantasy.premierleague./drf/bootstrap-static

var request = require('request'); // install: npm i request
var fs = require('fs');
var args = require('system').args;

request.get({url: 'https://fantasy.premierleague./drf/bootstrap-static'}, function(err, response, body) {
  console.log(body);
});

How I found this url?

Simple:

发布评论

评论列表(0)

  1. 暂无评论