我在向请求添加超过1个cookie时遇到问题,这是我的1个cookie的javascript:
var page = require('webpage').create();
phantom.addCookie({
'name' : 'TestCookie_Name_201312174009', /* required property */
'value' : 'TestCookie_Value_164009', /* required property */
'domain' : 'www.miau.com', /* required property */
'path' : '/',
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (1000 * 60 * 60) /* <-- expires in 1 hour */
});
page.open('http://www.miau.com', function() {
setTimeout(function() {
//page.render('google.png');
phantom.exit();
}, 200);
});
我通过代理启动它以查看请求:
phantomjs --ignore-ssl-errors=true --disk-cache=true --proxy=http://127.0.0.1:8080 --web-security=false test.js
cookie添加正常,但后来我尝试2个cookie:
并且它不起作用,我也尝试了这个其他选项,将其视为列表
var page = require('webpage').create();
phantom.addCookie([{
'name' : 'TestCookie_Name_201312174009', /* required property */
'value' : 'TestCookie_Value_164009', /* required property */
'domain' : 'www.miau.com', /* required property */
'path' : '/',
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (1000 * 60 * 60) /* <-- expires in 1 hour */
}
,
{
'name' : 'TestCookie_Name_2', /* required property */
'value' : 'TestCookie_Value_2', /* required property */
'domain' : 'www.miau.com', /* required property */
'path' : '/',
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (1000 * 60 * 60) /* <-- expires in 1 hour */
}]);
但同样,我无法让它发挥作用......
我尝试过的另一件事是:
var page = require('webpage').create();
phantom.addCookie({
'name' : 'TestCookie_Name_201312174009', /* required property */
'value' : 'TestCookie_Value_164009', /* required property */
'domain' : 'www.miau.com', /* required property */
'path' : '/',
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (1000 * 60 * 60) /* <-- expires in 1 hour */
});
phantom.addCookie({
'name' : 'TestCookie_Name_2', /* required property */
'value' : 'TestCookie_Value_2', /* required property */
'domain' : 'www.miau.com', /* required property */
'path' : '/',
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (1000 * 60 * 60) /* <-- expires in 1 hour */
});
page.open('http://www.miau.com', function() {
setTimeout(function() {
//page.render('google.png');
phantom.exit();
}, 200);
});
看看PhantomJS文档:
addCookie(Object) {Boolean}
介绍:PhantomJS 1.7
将Cookie添加到CookieJar。如果成功添加,则返回
true
,否则返回false
。
它不是指多个cookie。所以看看phantom.cookies
这是变量,它持有cookiejar - 我们发现以下内容:
phantom.cookies {Object[]}
介绍:PhantomJS 1.7
获取或设置任何域的Cookie(但是,设置时,首选使用phantom.addCookie)。这些Cookie存储在CookieJar中,并在打开相关的WebPages时提供。
此数组将由存储在PhantomJS启动配置/命令行选项中指定的cookie文件中的任何现有Cookie数据预先填充(如果有)。
上面的文档引用告诉我们,幻像对象中的cookies变量是一个对象数组。所以必须分配不止一个。通过快速查看测试,我们注意到,有一个分配多个cookie的测试 - 请参阅github
的引用代码行:
基本上告诉我们,只需调用即可分配多个cookie:
phantom.cookies = [{
'name' : 'TestCookie_Name_201312174009', /* required property */
'value' : 'TestCookie_Value_164009', /* required property */
'domain' : 'www.miau.com', /* required property */
'path' : '/',
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (1000 * 60 * 60) /* <-- expires in 1 hour */
}
,
{
'name' : 'TestCookie_Name_2', /* required property */
'value' : 'TestCookie_Value_2', /* required property */
'domain' : 'www.miau.com', /* required property */
'path' : '/',
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (1000 * 60 * 60) /* <-- expires in 1 hour */
}];