【问题标题】:How do I set proxy in phantomjs如何在phantomjs中设置代理
【发布时间】:2015-04-18 17:52:53
【问题描述】:

这个https://www.npmjs.com/package/phantom#functionality-details 页面说:

您还可以通过为 phantom.create() 指定附加参数,将命令行开关传递给 phantomjs 进程,例如:

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

或通过在 options* 对象中指定它们:

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

这些示例仅在咖啡脚本中,并且它们暗示 create 函数可以使用

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'}}

文档称该对象为 options* 对象 *见上文^

'--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() {

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

这不会产生错误,页面会被抓取,但代理会被忽略。

【问题讨论】:

  • 代理设置是在进程创建期间设置的,而不是在页面打开期间设置的。您是否真的尝试将字符串或对象作为第一个参数传递给phantom.create?如何验证代理设置不起作用?
  • 将字符串作为 page.create 的第一个参数传递会产生错误(预期的函数),我尝试按照您的建议将其放在 phantom.create 上(在函数之前和之后尝试),没有错误但是它没有效果。我没有被视为在代理上
  • 是socks5代理吗?
  • 我认为它是来自us-proxy.org的http代理

标签: node.js proxy phantomjs


【解决方案1】:

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

【讨论】:

  • 刚刚尝试过,但它不起作用。页面加载但代理被忽略
  • 我刚刚注意到您的链接指向 phantomjs-node 这与我拥有的 package.json 不同> github.com/Medium/phantomjs
  • 刚装了你的那个却没有变化,很奇怪
  • 您正在使用指向我引用的存储库的phantom npm 包。另一方面,phantomjs npm 包是实际的 PhantomJS 可执行文件,而不是桥接器。
  • 那我应该用什么,我很困惑? phantom.setProxie('98.239.198.83','21320','http'); TypeError: Object # 没有方法 'setProxie'
【解决方案2】:
{ parameters: { 'proxy': 'socks://98.239.198.83:21320' } }

他们没有更新他们的文档。

【讨论】:

  • 你在说什么文档? http--proxy-type 下文档中的默认设置:phantomjs.org/api/command-line.html
  • node-phantom .. 方法 --proxy=192.168.1.42:8080 --proxy-type=[http|socks5|none] 已在参数中组合,我在长时间的讨论中看到。
  • 代理身份验证参数怎么样?
【解决方案3】:
var phantom = require('phantom');
phantom.create(function (browser) {
    browser.setProxy(proxyIP, proxyPort);
    page.open(url, function (status) {
        console.log(status);
    });
},{dnodeOpts:{weak: false}});

它在我的窗户上运行良好。

【讨论】:

    【解决方案4】:

    作为尝试找出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 的结果是:

    页面打开了吗?成功

    页面标题是恭喜。此浏览器配置为使用 Tor。

    【讨论】:

      【解决方案5】:

      至于 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();
            });
      

      【讨论】:

        【解决方案6】:

        时间在流逝,所以 PhantomJS 现在能够“即时”设置代理(甚至基于每页):请参阅此提交:https://github.com/ariya/phantomjs/commit/efd8dedfb574c15ddaac26ae72690fc2031e6749

        这里是新 setProxy 函数的示例用法(我没有找到网页设置用法,这是代理在幻像实例上的一般用法):

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

        如果您想要每页代理,请使用代理的完整 URL(架构、用户名、密码、主机、端口 - 全部是 URL)

        【讨论】:

          【解决方案7】:

          使用 phantom npm 包和 con npm 包。

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

          【讨论】:

            【解决方案8】:

            我正在从 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();
            });
            

            【讨论】:

              【解决方案9】:

              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);
              

              这是一个在特定页面中设置代理的选项。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2013-01-19
                • 1970-01-01
                • 2017-07-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多