I was wondering how we could set cli parameters inside our code and not by placing them at the end of our mand 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 to check that everything is right (I'm writing bots that will frequently change their proxies).
I was wondering how we could set cli parameters inside our code and not by placing them at the end of our mand 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. to check that everything is right (I'm writing bots that will frequently change their proxies).
Share Improve this question edited Jan 31, 2014 at 10:07 Brice Favre 1,5271 gold badge17 silver badges36 bronze badges asked Jan 31, 2014 at 8:15 Julien Le CoupanecJulien Le Coupanec 7,9969 gold badges56 silver badges70 bronze badges 2- 1 You can add a config array in create but proxy and proxy-type are phantomjs options. – Brice Favre Commented Jan 31, 2014 at 10:07
- Thank you! Do you know how I can access them? – Julien Le Coupanec Commented Jan 31, 2014 at 11:12
3 Answers
Reset to default 5This 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 ment 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.
this works
casper = require('casper').create({
pageSettings: {
proxy: 'http://localhost:3128'
}
});
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',
/* ... */
},
/* ... */
},