2013-10-03 203 views
29

我使用PhantomJS做出一個網頁調用時指定用戶代理,像這樣:PhantomJS:撥打電話

page.open('http://example.com', function (s) { 
    console.log(page.content); 
    phantom.exit(); 
}); 

我在Drupal Simpletests的環境中使用這一點,這需要我設置一個特殊的USERAGENT爲了使用測試數據庫而不是實際的數據庫。我想提取特定用戶代理的網頁。例如,在使用Curl的PHP中,我可以在進行cUrl調用之前使用CURLOPT_USERAGENT完成此操作。

謝謝!

阿爾伯特

回答

49

我發現在useragent.js文件答案在phantomjs examples directory

var page = require('webpage').create(); 
console.log('The default user agent is ' + page.settings.userAgent); 
page.settings.userAgent = 'SpecialAgent'; 
page.open('http://www.httpuseragent.org', function (status) { 
    if (status !== 'success') { 
     console.log('Unable to access network'); 
    } else { 
     var ua = page.evaluate(function() { 
      return document.getElementById('myagent').innerText; 
     }); 
     console.log(ua); 
    } 
    phantom.exit(); 
}); 
+0

我越來越無法訪問網絡。我可以通過其他瀏覽器訪問該網站。它是內部訪問的網站。是否需要設置一些設置才能從Phantom訪問? – DoodleKana