3

Here is my code to check for IP address using http://whatismyipaddress.com/proxy-check

var casper = require("casper").create(),
    utils = require('utils');

casper.userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');

casper.start("http://whatismyipaddress.com/proxy-check", function() {
  this.capture('0.png');
});

casper.on('remote.message', function(msg) {
    this.echo('Remote: ' + msg);
})

casper.run(function() {
  this.echo('End');

  //finish execution script 
  this.exit();  

});

I typed casperjs --proxy="xxx.xxx.xxx.xxx:80" proxy.js But the screenshot is exactly the IP address from my machine.

So how do I know if the proxy is bad or if something wrong in my code or command line?

4

3 回答 3

5

如果要测试casperjs是否成功使用代理服务器,可以使用下面的脚本测试。它向 whatismyip.com 发出请求打印 casperjs 使用的 IP 地址。

//create casper object
var casper = require('casper').create();

casper.start('http://www.whatismyip.com/');

//to avoid 'Access Denied'  <!-- Error #1010 -->
//The owner of this website (www.whatismyip.com) has banned  
//your access based on your browser's signature (42c0a6c6-cl-ua-50). (Ref. 1010)
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');

//Home Page
casper.then(function() {

    if(this.exists('div#greenip')){

        var element = this.evaluate(function() {
            //remove unnecessari nodes
            return jQuery('div#greenip').text().trim();
        });

        console.log(element);   
    }else{
        console.log("ACCESS_DENIED");   
    }    

});

casper.run(function() {

    //finish execution script 
    this.exit();
});

输出:

casperjs --proxy=XXX.XXX.XXX.XXX:60099 --proxy-auth=username:password proxy.js
XXX.XXX.XXX.XXX

于 2013-05-30T21:10:20.627 回答
4

使用http://ip-addr.es/的更短的 casperjs 脚本

var casper = require('casper').create();
var url = 'http://ip-addr.es/';

casper.start(url, function() {
    var js = this.evaluate(function() {
        return document; 
    }); 
    this.echo(js.all[0].outerText); 
});
casper.run();

假设您已将之前的脚本保存为 whatismyip.js,并且您有一个 SOCKSv5 代理在 localhost 端口 8000 上侦听,那么您可以检查 casperjs 当前是否正在使用代理:

simone@tramontana:~$ casperjs  whatismyip.js 
93.[xx].[xx].226 # I've hidden a couple of bytes of my real IP ;)

simone@tramontana:~$ casperjs --proxy=127.0.0.1:8000 --proxy-type=socks5 whatismyip.js 
46.23.73.4
于 2014-03-05T15:12:22.060 回答
1

我运行了你的代码,它对我来说很好用

在没有代理的情况下运行脚本。

casperjs proxy.js

图像1

使用代理运行脚本。

casperjs --proxy=208.72.118.16:60099 --proxy-auth=username:password proxy.js

img2

于 2013-05-30T13:49:25.233 回答