2016-09-27 74 views
8

我有一個PhantomJS腳本時,我本地運行(蘋果機),但是當我在我的Linux服務器上運行它的工作原理,它返回以下錯誤:

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

的代碼是:

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

page.open('https://fantasy.premierleague.com/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(); 
}); 
+2

爲什麼不只是請求這個網址:https://fantasy.premierleague.com/drf/bootstrap-static?我沒有看到使用phantomjs的好處,因爲它實際上會自動請求API並獲取數據。 – num8er

+1

哇...我不相信實際存在,謝謝! –

+1

您使用哪個** PhantomJS版本**?請註冊onConsoleMessage,onError,onResourceError,onResourceTimeout事件([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file-1_phantomerrors-js))。也許有錯誤。 –

回答

-3

我不打算對給予好評。 我提供的解決方案可以解決某些情況,而無需使用phantomjs模擬瀏覽器行爲,只需檢索可通過請求url直接處理的數據。

您從頁面需要的數據,那麼,爲什麼不只是做請求這個網址:https://fantasy.premierleague.com/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.com/drf/bootstrap-static'}, function(err, response, body) { 
    console.log(body); 
}); 

如何,我發現這個網址是什麼?

簡單:

firebug inspect net

+1

請求包是爲node.js編寫的,不適用於PhantomJS。你可能幫助過OP,但你還沒有回答這個問題。 –

+0

親愛的@ArtjomB!我正在回答Phill需要的解決方案。他只需要從另一個站點獲取數據。這就是爲什麼我說當沒有直接的url將所有數據作爲json返回時,不需要phantomjs。 SO是我們幫助解決問題的地方。我的解決方案解決了解析數據的問題。我看到你是明智的,所以回答解決方案,而不是僅僅降低他人的答案和評論。這不是高尚人士的做法。 – num8er

+2

這仍然不會幫助那些想要使用PhantomJS並來到這裏*「ReferenceError:無法找到變量:$」*的人。這個問題不包含一個單詞來支持你的觀點,即OP只是想從頁面下載一些JSON而沒有其他的東西。投票是質量控制的一種形式。你自己的答案沒問題,但它與問題無關,因爲它被問到,我想至少讓未來的讀者感到沮喪。我覺得這樣做很高尚。 –

1

有可能是你的代碼和jQuery之間的競爭條件正在加載的頁面上。將page.evaluate回調中的語句換成$(document).ready(function() { /* your statements here */ });以確保頁面上的腳本已完全加載。

+0

)當'page.open'回調函數被調用時DOM已經準備就緒,並且'''*'只能在['page.evaluate']內部訪問(http://phantomjs.org/api /webpage/method/evaluate.html)。你不能在'page.evaluate' –

+0

之外使用'$',你不能使用$(document)。如果jQuery尚未加載 – Fer

相關問題