javascript CasperJS:在代码中配置代理选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21475273/
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
CasperJS: Configure proxy options inside code
提问by Julien Le Coupanec
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:
我想知道如何在代码中设置 cli 参数,而不是像这样将它们放在命令的末尾:
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).
我想要实现的是在我的代码中设置新的代理并从 whatsmyip.com 中删除我的新 IP 地址以检查一切是否正确(我正在编写会经常更改其代理的机器人)。
采纳答案by Julien Le Coupanec
I needed CasperJS to run inside a node environment. So I have set up Spookyand the good news is that you can set one inside your code like this:
我需要 CasperJS 在节点环境中运行。所以我已经设置了 Spooky,好消息是你可以在你的代码中设置一个,如下所示:
var spooky = new Spooky({
child: {
proxy: '192.128.101.42:9001',
/* ... */
},
/* ... */
},
回答by Tobia
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?
如何编写由 CasperJS 使用特定 CLI 选项运行的单个脚本?
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.
使用单个脚本文件没有干净的解决方案,因为“shebang”行#!/bin/...
非常有限。事实上,在大多数操作系统上,它只支持解释器名称后的单个参数。
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.
“正确”的解决方案当然是使用多个脚本,通常是一个 bash 脚本,它将使用适当的选项执行您的 CasperJS 脚本。
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 itselfand then terminate. CasperJS will skip over the line aimed for Bash and run the rest of the script.
在这种情况下,由于 shebang 行,脚本将首先由 Bash 读取。该脚本将指示 Bash 使用脚本本身的特定选项执行 CasperJS ,然后终止。CasperJS 将跳过针对 Bash 的行并运行脚本的其余部分。
JavaScript version
JavaScript 版本
#!/bin/sh
//bin/true; exec casperjs --proxy=127.0.0.1:8003 test "#!/bin/sh
""""exec casperjs --proxy=127.0.0.1:8003 test "casper = require('casper').create({
pageSettings: {
proxy: 'http://localhost:3128'
}
});
" "$@" #"""
(rest of CoffeeScript file)
" "$@"
(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.
这里的技巧是//
在 Javascript中开始注释,而在 Bash 中它只是第一行代码的一部分。
CoffeeScript version
CoffeeScript 版本
##代码##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.
这里的技巧是""""
Bash 跳过了它,因为它只是两个空字符串,而在 CoffeeScript 中它打开了一个多行字符串,它吞下了第一行代码。
回答by Dir Indesa
this works
这有效
##代码##