2016-09-29 20 views
0

我有一個簡單的腳本基於關閉Phantomjs的網絡監控example試圖記錄所有的請求,並與特定網站相關聯的響應頭:Phantomjs不加載正確的URL裏面onResourceRequested

"use strict"; 

var page = require('webpage').create(), 
    system = require('system'), 
    address; 

if (system.args.length === 1) { 
    console.log('Usage: getRequests.js <some URL>'); 
    phantom.exit(1); 
} else { 

    address = system.args[1]; 

    console.log('The url entered is ' + address + '\n') 

    page.onResourceReceived = function(response) { 
     console.log('Received Request: ' + JSON.stringify(response.headers, undefined, 4) + '\n'); 
    }; 

    page.onResourceRequested = function (req) { 
     console.log('Request URL ' + JSON.stringify(req.url, undefined, 4) + "\n"); 

    } 

    page.onResourceError = function(resourceError) { 
     console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')'); 
     console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString); 
    }; 

    page.open(address); 


} 

然而,當我嘗試運行它,使用./phantomjs getRequests.js www.google.com(或任何其他網頁),它給人的錯誤:

The url entered is www.google.com 

Request URL "file:///home/myusername/scripts/www.google.com" 

Unable to load resource (#1URL:file:///home/myusername/scripts/www.google.com) 
Error code: 203. Description: Error opening /home/myusername/scripts/www.google.com: No such file or directory 
Received Request: [] 

基本上,我是在終端上輸入URL被前綴與腳本文件的路徑。我在地址變量中輸入的內容非常精細,例如google.com

任何人都可以請幫助解決這個問題嗎?我不明白爲什麼phantomjs可能會這樣做。我在VirtualBox上運行Ubuntu 14.04。當我剛剛嘗試輸出特定頁面的請求時,腳本運行正常。 謝謝。

回答

1

你可能會笑,但使用的腳本正確的方法是

./phantomjs getRequests.js http://www.google.com 
+0

天啊,太感謝你了!我在這上面花了一個小時。 – QPTR