CasperJS: Configure proxy options inside code

拈花ヽ惹草 提交于 2019-12-20 15:33:49

问题


I was wondering how we could set cli parameters inside our code and not by placing them at the end of our command like this:

casperjs casper_tor.js --proxy=127.0.0.1:9050 --proxy-type=socks5

I've tested things like that but it didn't work:

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

casper.cli.options["proxy"] = "127.0.0.1:9050";
casper.cli.options["proxy-type"] = "socks5";
...
casper.run();

What I'm trying to achieve is to set new proxies inside my code and to scrap my new ip address from whatsmyip.com to check that everything is right (I'm writing bots that will frequently change their proxies).


回答1:


This is not strictly an answer to your question, but to the more general:

How can I write a single script that will be run by CasperJS using specific CLI options?

There is no clean solution using a single script file, because the "shebang" line #!/bin/... is very limited. In fact, on most OS it only supports a single argument after the interpreter name.

The "proper" solution is of course using more than one script, usually a bash script that will execute your CasperJS script with the proper options.

But...

There is a very old  trick  horrible hack that addresses this problem, the polyglot script. It involves misusing language features to write a file that is a valid script in two (or more) interpreters, doing two different things.

In this case the script will first be read by Bash, because of the shebang line. The script will direct Bash to execute CasperJS with specific options on the script itself and then terminate. CasperJS will skip over the line aimed for Bash and run the rest of the script.

JavaScript version

#!/bin/sh
//bin/true; exec casperjs --proxy=127.0.0.1:8003 test "$0" "$@"

(rest of JavaScript file)

The trick here is that // starts a comment in Javascript, while in Bash it's just part of the first line of code.

CoffeeScript version

#!/bin/sh
""""exec casperjs --proxy=127.0.0.1:8003 test "$0" "$@" #"""

(rest of CoffeeScript file)

The trick here is that """" is skipped over by Bash, because it's just two empty strings, while in CoffeeScript it opens a multiline string that swallows the first line of code.




回答2:


this works

casper = require('casper').create({
        pageSettings: {
            proxy: 'http://localhost:3128'
         }
});



回答3:


I needed CasperJS to run inside a node environment. So I have set up Spooky and the good news is that you can set one inside your code like this:

var spooky = new Spooky({
        child: {
            proxy: '192.128.101.42:9001',
            /* ... */
        },
        /* ... */
    },


来源:https://stackoverflow.com/questions/21475273/casperjs-configure-proxy-options-inside-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!