这个 https://www.npmjs.com/package/phantom#functionality-details页面说:
您还可以通过为 phantom.create() 指定附加参数,将命令行开关传递给 phantomjs 进程,例如:
phantom.create '--load-images=no', '--local-to-remote-url-access=yes', (page) ->
或通过在 options* 对象中指定它们:
phantom.create {parameters: {'load-images': 'no', 'local-to-remote-url-access': 'yes'}}, (page) ->
这些示例仅在 CoffeeScript 中,并且它们暗示 create 函数可以使用
create('string',function)
或
create([object object],function)
但真正期望的第一个参数是函数!
我真的很想试试http://phantomjs.org/api/command-line.html我可能有错误的想法,但对我来说,它们似乎可以在创建函数中使用(就在你执行 createPage 之前),我错了吗?
我尝试了几件事,最合乎逻辑的一个是:
var phantom = require('phantom');
phantom.create(function(browser){
browser.createPage(function(page){
page.open('http://example.com/req.php', function() {
});},{parameters:{'proxy':'98.239.198.83:21320'}});});
所以页面被打开了。我知道这一点,因为我让 req.php 将 $_SERVER 对象保存到 txt 垫,但是 REMOTE_ADDR 和 REMOTE_PORT header 不是我设置的代理中的 header 。它们没有效果。我也试过:
{options:{'proxy':'98.239.198.83:21320'}}
文档称该对象为 options* 对象 *见上文^
和
'--proxy=98.239.198.83:21320'
我还对幻像模块进行了挖掘以找到创建功能。它不是用js写的我至少看不到它。它必须在 C++ 中。看起来这个模块已经更新了,但是模块深处的例子看起来像旧代码。
我该怎么做?
编辑:
var phantom = require('phantom');
phantom.create(function(browser){
browser.createPage(function(page){
browser.setProxy('98.239.198.83','21320','http', null, null, function(){
page.open(
'http://example.com/req.php', function() {
});});});});
这不会产生错误,页面会被抓取,但代理会被忽略。
最佳答案
至于 phantom 2.0.10 版本,以下代码在我的 windows 机器上运行良好
phantom.create(["--proxy=201.172.242.184:15124", "--proxy-type=socks5"])
.then((instance) => {
phInstance = instance;
return instance.createPage();
})
.then((page) => {
sitepage = page;
return page.open('http://newsdaily.online');
})
.then((status) => {
console.log(status);
return sitepage.property('title');
})
.then((content) => {
console.log(content);
sitepage.close();
phInstance.exit();
})
.catch((error) => {
console.log(error);
phInstance.exit();
});
关于node.js - 如何在phantomjs中设置代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28571601/