如何从cmd

时间:2017-02-03 14:38:31

标签: amazon-ec2 phantomjs

如何将用户代理定义提供给 phantomjs ,我目前正在 aws 服务器 ec2 实例上使用以下命令运行

phantomjs --web-security=no --ssl-protocol=any --ignore-ssl-errors=true driver.js http://example.com

1 个答案:

答案 0 :(得分:1)

您只能在脚本中设置PhantomJS中的用户代理(在您的示例中为driver.js)。有关它的文档:http://phantomjs.org/api/webpage/property/settings.html

如果要在命令行中将用户代理传递给PhantomJS,可以使用参数。在脚本中,您可以获取参数并将其设置为用户代理。您可以尝试以下示例:

var webPage = require('webpage');
var system = require('system');

var page = webPage.create();

var userAgent = system.args[1];

page.settings.userAgent = userAgent;

console.log('user agent: ' + page.settings.userAgent);

phantom.exit();

按如下方式运行:

  

$ phantomjs ua.js“Mozilla / 5.0(Windows NT 6.1; WOW64)   AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 37.0.2062.120   Safari浏览器/ 537.36"

你会得到输出:

  

用户代理:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36   (KHTML,与Gecko一样)Chrome / 37.0.2062.120 Safari / 537.36