node.js 如何在phantomjs中设置代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28571601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set proxy in phantomjs
提问by Ben Muircroft
This https://www.npmjs.com/package/phantom#functionality-detailspage says:
这个https://www.npmjs.com/package/phantom#functionality-details页面说:
You can also pass command line switches to the phantomjs process by specifying additional args to phantom.create(), eg:
您还可以通过向 phantom.create() 指定其他参数来将命令行开关传递给 phantomjs 进程,例如:
phantom.create '--load-images=no', '--local-to-remote-url-access=yes', (page) ->
or by specifying them in the options*object:
或者通过在options*对象中指定它们:
phantom.create {parameters: {'load-images': 'no', 'local-to-remote-url-access': 'yes'}}, (page) ->
These examples are only in coffee script and also they insinuate that the create function can take
这些示例仅在咖啡脚本中,并且它们暗示 create 函数可以采用
create('string',function)
or
或者
create([object object],function)
but really the first parameter expected is the function!
但真正预期的第一个参数是函数!
I really wanted to try http://phantomjs.org/api/command-line.htmlI might have the wrong idea but to me it looks like they can be used in the create function (right before you do the createPage), am I wrong?
我真的很想尝试http://phantomjs.org/api/command-line.html我可能有错误的想法但对我来说看起来它们可以在 create 函数中使用(就在你执行 createPage 之前),我我错了?
I have tried several things, the most logical one is this:
我尝试了几件事,最合乎逻辑的一件是:
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'}});});
So the page gets opened. I know this because I am making req.php save the $_SERVER object to a txt pad but, the REMOTE_ADDR and REMOTE_PORT headers are not the ones in the proxy I have set. They have no effect. I have also tried:
所以页面被打开了。我知道这一点是因为我让 req.php 将 $_SERVER 对象保存到 txt pad,但是 REMOTE_ADDR 和 REMOTE_PORT 标头不是我设置的代理中的标头。它们没有效果。我也试过:
{options:{'proxy':'98.239.198.83:21320'}}
As the docs call that object the options*object *see above^
由于文档称该对象为options*object *see above^
and
和
'--proxy=98.239.198.83:21320'
I have also had a dig through the phantom module to find the create function. It is not written in js I can't see it at least. It must be in C++. It looks like this module has been updated but, the examples deep inside the module look like old code.
我还深入研究了 phantom 模块以找到 create 函数。它不是用 js 编写的,至少我看不到。它必须在 C++ 中。看起来这个模块已经更新,但是模块内部的示例看起来像旧代码。
How do I do this?
我该怎么做呢?
EDIT:
编辑:
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() {
});});});});
This produces no error and the page gets scraped but the proxy is ignored.
这不会产生错误并且页面被抓取但代理被忽略。
采纳答案by Ben Muircroft
{ parameters: { 'proxy': 'socks://98.239.198.83:21320' } }
They didn't update their docs.
他们没有更新他们的文档。
回答by Suben Saha
As for as phantom 2.0.10 version the following code is running very well in my windows machine
至于 phantom 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();
});
回答by deksden
Time is going on, so PhantomJS now able to set proxy "on the fly" (even on per-page-basis): see this commit: https://github.com/ariya/phantomjs/commit/efd8dedfb574c15ddaac26ae72690fc2031e6749
时间在流逝,因此 PhantomJS 现在可以“即时”设置代理(即使是基于每页):请参阅此提交:https: //github.com/ariya/phantomjs/commit/efd8dedfb574c15ddaac26ae72690fc2031e6749
Here is sample usage of new setProxy function (i did not find web page setting usage, this is general usage of proxy on instance of phantom):
这是新的 setProxy 函数的示例用法(我没有找到网页设置用法,这是 phantom 实例上代理的一般用法):
https://github.com/ariya/phantomjs/blob/master/examples/openurlwithproxy.js
https://github.com/ariya/phantomjs/blob/master/examples/openurlwithproxy.js
If you want per-page proxy, use full URL for proxy (schema, user name,password, host, port - all it the URL)
如果您想要每页代理,请使用代理的完整 URL(架构、用户名、密码、主机、端口 - 所有这些都是 URL)
回答by qinggangxu
use phantom npm package and co npm package.
使用 phantom npm 包和 co npm 包。
co(function*() {
const phantomInstance = yield phantom.create(["--proxy=171.13.36.64:808"]);
crawScheduler.start(phantomInstance);
});
回答by dmmfll
As a side effect of trying to figure out an issue on Github for phantomjs-nodejsI was able to set a proxy as follows:
作为尝试在 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()
The result where the proxy uses Tor was:
代理使用 Tor 的结果是:
page opened? success
Page title is Congratulations. This browser is configured to use Tor.
页面打开了?成功
页面标题是祝贺。此浏览器配置为使用 Tor。
回答by Salem
I'm running PhantomJS from windows cmd and syntaxes it use looks bit different from what's I notice if you didn't put http://PJ wont recognize the value this is complete example
我正在从 windows cmd 运行 PhantomJS,它使用的语法看起来与我注意到的有点不同,如果你没有把http://PJ 识别出这个值,这是完整的例子
var page = require('webpage').create();
page.settings.loadImages = false; //
page.settings.proxy = 'http://192.168.1.5:8080' ;
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.open('http://some.com/page', function() {
page.render('some.png');
phantom.exit();
});
回答by Bars
Yet another solution for nodejs:
nodejs 的另一种解决方案:
const phantomInstance = await require('phantom').create();
const page = await phantomInstance.createPage();
// get current settings:
var pageSettings = await page.property('settings');
/*
{
XSSAuditingEnabled: false,
javascriptCanCloseWindows: true,
javascriptCanOpenWindows: true,
javascriptEnabled: true,
loadImages: true,
localToRemoteUrlAccessEnabled: false,
userAgent: 'Mozilla/5.0 (Unknown; Linux x86_64) ... PhantomJS/2.1.1 Safari/538.1',
webSecurityEnabled: true
}
*/
pageSettings.proxy = 'https://78.40.87.18:808';
// update settings (return value is undefined):
await page.property('settings', pageSettings);
const status = await page.open('https://2ip.ru/');
// show IP:
var ip = await page.evaluate(function () {
var el = document.getElementById('d_clip_button');
return !el ? '?' : el.textContent;
});
console.log('IP:', ip);
It's an option to set proxy within specific page.
这是在特定页面内设置代理的选项。
回答by Artjom B.
The CoffeeScript example is a little strange, because it is the browserthat is passed into the callback of phantom.createand not page, but otherwise it must be compatible judging by the code.
CoffeeScript 的例子有点奇怪,因为它是browser传递到phantom.createand not的回调中的page,否则从代码判断它必须是兼容的。
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() {
...
});
});
});
Proxy settings are set during process creation, not during page opening. Although PhantomJS contains an undocumented phantom.setProxy()function which enables you to change the proxy settings in the middle of the script. The phantom module also seems to support it.
代理设置是在流程创建期间设置的,而不是在页面打开期间设置。尽管 PhantomJS 包含一个未公开的phantom.setProxy()函数,它使您可以在脚本中间更改代理设置。幻像模块似乎也支持它。
回答by Ben Zhong
var phantom = require('phantom');
phantom.create(function (browser) {
browser.setProxy(proxyIP, proxyPort);
page.open(url, function (status) {
console.log(status);
});
},{dnodeOpts:{weak: false}});
it works fine on my windows.
它在我的窗户上运行良好。

