2015-02-17 43 views
9
設置代理

https://www.npmjs.com/package/phantom#functionality-details頁說:我如何在phantomjs

您也可以通過命令行指定額外的參數,以phantom.create(切換到phantomjs過程),例如:

phantom.create '--load-images=no', '--local-to-remote-url-access=yes', (page) -> 

或在選項指定它們*對象:

phantom.create {parameters: {'load-images': 'no', 'local-to-remote-url-access': 'yes'}}, (page) -> 

這些例子只是在咖啡劇本,他們也認爲影射在創建函數可以接受

create('string',function) 

create([object object],function) 

但真正期待的第一個參數是功能!

我真的很想嘗試http://phantomjs.org/api/command-line.html我可能有錯誤的想法,但對我來說,它看起來像他們可以用在創建功能(就在你做createPage之前),我錯了嗎?

我已經試過幾件事情,最合乎邏輯的一個是這樣的:

var phantom = require('phantom'); 
phantom.create(function(browser){ 
    browser.createPage(function(page){ 
     page.open('http://example.com/req.php', function() { 

      });},{parameters:{'proxy':'98.239.198.83:21320'}});}); 

所以頁面被打開。我知道這一點,因爲我正在使req.php保存$ _SERVER對象到一個txt墊,但是,REMOTE_ADDR和REMOTE_PORT頭不是我設置的代理中的頭。他們沒有效果。我也曾嘗試:

{options:{'proxy':'98.239.198.83:21320'}} 

由於文檔調用該對象的選項*對象*見上^

'--proxy=98.239.198.83:21320' 

我也通過幻影模塊有一個挖找到創建功能。它不是用js寫的,我至少看不到它。它必須在C++中。它看起來像這個模塊已經更新,但模塊內部的例子看起來像舊代碼。

我該怎麼做?

編輯:

var phantom = require('phantom'); 
phantom.create(function(browser){ 
    browser.createPage(function(page){ 

    browser.setProxy('98.239.198.83','21320','http', null, null, function(){ 

    page.open(
     'http://example.com/req.php', function() { 

     });});});}); 

這會產生錯誤並且頁面被刮掉,但代理將被忽略。

+0

代理設置在創建流程期間設置,而不是在頁面打開期間設置。你真的試圖將字符串或對象作爲第一個參數傳遞給'phantom.create'嗎?你如何驗證代理設置不起作用? – 2015-02-17 21:48:19

+1

傳遞字符串作爲page.create的第一個參數給出了錯誤(期望的函數),我試着按照你的推理將它放在phantom.create上(在函數前後嘗試),沒有錯誤,但它沒有效果。我沒有被視爲代理 – 2015-02-17 21:59:05

回答

4
{ parameters: { 'proxy': 'socks://98.239.198.83:21320' } } 

他們沒有更新他們的文檔。

+0

你在說什麼文檔?'http'是'--proxy-type'下文檔的默認設置:http: //phantomjs.org/api/command-line.html – 2015-02-23 19:40:51

+1

什麼是代理認證參數? – fiveDust 2017-08-19 16:52:06

0

的CoffeeScript的例子是有些奇怪,因爲它是傳遞到的phantom.create回調browser而不是page,但除此之外,它必須是由code兼容判斷。

var phantom = require('phantom'); 
phantom.create({ 
    parameters: { 
     proxy: '98.239.198.83:21320' 
    } 
}, function(browser){ 
    browser.createPage(function(page){ 
     page.open('http://example.com/req.php', function() { 
      ... 
     }); 
    }); 
}); 

代理設置在創建過程中設置,而不是在頁面打開期間設置。雖然PhantomJS包含一個未公開的phantom.setProxy()函數,該函數使您可以在腳本中間更改代理設置。幻影模塊似乎也是support it

+0

的http代理,只是試過這個,但它不起作用。頁面加載但代理服務器被忽略 – 2015-02-17 22:02:42

+0

我剛剛注意到你的鏈接指向phantomjs-node,它與我所擁有的package.json不同 2015-02-17 22:08:07

+0

剛剛安裝了一個youve但沒有變化,很奇怪 – 2015-02-17 22:13:19

-1
var phantom = require('phantom'); 
phantom.create(function (browser) { 
    browser.setProxy(proxyIP, proxyPort); 
    page.open(url, function (status) { 
     console.log(status); 
    }); 
},{dnodeOpts:{weak: false}}); 

它在我的窗戶上正常工作。

2

由於試圖找出一個issue on Github for phantomjs-nodejs我能夠設置一個代理如下副作用:

phantom = require 'phantom' 
parameters = { 
    loadimages: '--load-images=no', 
    websecurity: '--web-security=no', 
    ignoresslerrors: '--ignore-ssl-errors=yes', 
    proxy: '--proxy=10.0.1.235:8118', 
} 
urls = { 
    checktor: "https://check.torproject.org/", 
    google: "https://google.com", 
} 

phantom.create parameters.websecurity, parameters.proxy, (ph) -> 
    ph.createPage (page) -> 
    page.open urls.checktor, (status) -> 
     console.log "page opened? ", status 
     page.evaluate (-> document.title), (result) -> 
     console.log 'Page title is ' + result 
     ph.exit() 

在代理服務器使用結果Tor的是:

page opened? success

Page title is Congratulations. This browser is configured to use Tor.

2

時間我所以PhantomJS現在能夠設置代理「即時」(即使在每頁基礎上):看到這個提交:https://github.com/ariya/phantomjs/commit/efd8dedfb574c15ddaac26ae72690fc2031e6749

下面是新的setProxy函數的示例用法(我沒有找到網頁設置使用,這是幻象的實例代理的一般用法):

https://github.com/ariya/phantomjs/blob/master/examples/openurlwithproxy.js

如果你想每個頁面的代理,使用完整的URL代理(架構,用戶名,密碼,主機,端口 - 所有它的URL)

2

使用phantom npm包和co npm包。

co(function*() { 
    const phantomInstance = yield phantom.create(["--proxy=171.13.36.64:808"]); 
    crawScheduler.start(phantomInstance); 
});