我如何在phantomjs中设置代理
这https://www.npmjs.com/package/phantom#functionality-details页面说:
您还可以通过指定其他参数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) ->
这些例子只在咖啡脚本中,也暗示了创build函数可以采用
create('string',function)
要么
create([object object],function)
但真正的第一个参数是预期的function!
我真的想尝试http://phantomjs.org/api/command-line.html我可能有错误的想法,但对我来说,它看起来像他们可以用于创buildfunction(就在你做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'
我也通过幻影模块挖掘find创buildfunction。 它不是写在JS我至less看不到它。 它必须在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() { });});});});
这不会产生错误,页面被刮掉,但代理被忽略。
{ parameters: { 'proxy': 'socks://98.239.198.83:21320' } }
他们没有更新他们的文档。
作为试图找出Github上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的结果是:
页面打开了? 成功
页面标题是恭喜。 这个浏览器被configuration为使用Tor。
至于幻影2.0.10版本,下面的代码在我的Windows机器上运行得非常好
phantom.create(["--proxy=201.172.242.184:15124", "--proxy-type=socks5"]) .then((instance) => { phInstance = instance; return instance.createPage(); }) .then((page) => { sitepage = page; return page.open('http://newsdaily.online'); }) .then((status) => { console.log(status); return sitepage.property('title'); }) .then((content) => { console.log(content); sitepage.close(); phInstance.exit(); }) .catch((error) => { console.log(error); phInstance.exit(); });
时间正在进行,所以PhantomJS现在能够设置代理“即时”(即使在每页的基础上):看到这个提交: https : //github.com/ariya/phantomjs/commit/efd8dedfb574c15ddaac26ae72690fc2031e6749
下面是新的setProxy函数的示例用法(我没有find网页设置的用法,这是幻像实例上的代理的一般用法):
https://github.com/ariya/phantomjs/blob/master/examples/openurlwithproxy.js
如果你想要每个页面的代理,使用完整的URL代理(模式,用户名,密码,主机,端口 – 所有的URL)
使用phantom npm软件包和co npm软件包。
co(function*() { const phantomInstance = yield phantom.create(["--proxy=171.13.36.64:808"]); crawScheduler.start(phantomInstance); });
CoffeeScript的例子有点奇怪,因为它是browser
被传入phantom.create
而不是page
的callback,否则它必须由代码来兼容判断。
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() { ... }); }); });
代理设置在创build过程中设置,而不是在页面打开期间设置。 虽然PhantomJS包含一个未phantom.setProxy()
函数,它使您可以在脚本中间更改代理设置。 幻影模块似乎也支持它 。
var phantom = require('phantom'); phantom.create(function (browser) { browser.setProxy(proxyIP, proxyPort); page.open(url, function (status) { console.log(status); }); },{dnodeOpts:{weak: false}});
它在我的窗户上工作正常。