我在向请求中添加多个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添加得很好,但是接下来我尝试了两个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);
});发布于 2019-04-01 11:15:20
查看PhantomJS文档:
addCookie(Object) {Boolean}介绍: PhantomJS 1.7 在CookieJar中添加一个Cookie。如果成功添加,则返回true,否则返回false。
它指的不是一个以上的曲奇。因此,看看phantom.cookies,它是一个变量,它保存了cookiejar -我们发现如下:
phantom.cookies {Object[]}介绍: PhantomJS 1.7 为任何域获取或设置Cookies (不过,对于设置,最好使用phantom.addCookie )。这些Cookies存储在CookieJar中,并将在打开相关WebPages时提供。 该数组将由存储在PhantomJS启动配置/命令行选项(如果有的话)中指定的Cookie文件中的任何现有cookie数据预先填充。
上面的文档引号告诉我们,幻影对象内部的cookie变量是一个对象数组。因此,必须有可能分配多个。通过快速查看测试,我们注意到,有一个用于分配多个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 */
}];https://stackoverflow.com/questions/55453384
复制相似问题