gpt4 book ai didi

javascript - 无法从 JSON 在 Phantomjs 中添加 cookie

转载 作者:行者123 更新时间:2023-11-29 19:44:49 31 4
gpt4 key购买 nike

我正在尝试使用 PhantomJS 动态加载一些 cookie,但出现了一些错误。这是我的代码:

var page = require('webpage').create();

var cookieJson = require('cookie.json'); // local cookie file

phantom.cookiesEnabled = true; // Enable Cookies

phantom.clearCookies(); // Clear Old Cookies

for(var i = 0; i< cookieJson.length; i++) { //for each domain, try to add the cookie
var temp = cookieJson[i];
console.log(JSON.stringify(temp)); // This seems to print just fine
phantom.addCookie(temp); // this throws an exception
}


phantom.exit();

以上代码抛出如下异常:

incompatible type of argument(s) in call to addCookie(); candidates were
addCookie(QVariantMap)

我确信这里有一个简单的解决方案,但我的大脑卡住了。我的印象是 JSON.stringify 从 JSON 对象返回一个字符串。真正令人困惑的是,当我将它打印到控制台时,它看起来与我将它存储为 String 完全一样。我的数据如下所示:

{"domain": ".sweatytacobo.com",
"expires": "Tue, 10 Jun 2014 16:37:46 GMT",
"expiry": ,
"httponly": false,
"name": "__utmz",
"path": "/",
"secure": false,
"value": "268881515.13222266.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"}

当我将上面的内容用作字符串时,它可以毫无问题地添加。那么,为什么我的 JSON.Stringify 给我带来问题?

编辑:

根据PhantomJS source code的评论addCookie 以以下格式传递给 QVariantMap:

 {
* "name" : "cookie name (string)",
* "value" : "cookie value (string)",
* "domain" : "cookie domain (string)",
* "path" : "cookie path (string, optional)",
* "httponly" : "http only cookie (boolean, optional)",
* "secure" : "secure cookie (boolean, optional)",
* "expires" : "expiration date (string, GMT format, optional)"
* }

那么我不应该能够以某种方式传递 JSON 对象吗?

最佳答案

好吧,我明白了。 PhantomJS addCookie 对 cookie 的格式非常挑剔。

基本上,为了转换 JSON,您必须通过遍历 JSON 对象来提取值。例如:

var cookieJson = require('cookiefile.json'); 
// load in the cookies in JSON format from file

for(var i = 0; i< cookieJson.length; i++) {
var tempCookie = {};

tempCookie["name"] = cookieJson[i]["name"];
tempCookie["domain"] = cookieJson[i]["domain"];
tempCookie["value"] = cookieJson[i]["value"];
tempCookie["path"] = cookieJson[i]["path"];
tempCookie["session"] = cookieJson[i]["session"];
tempCookie["storeId"] = cookieJson[i]["storeId"];

// Here we are adding the relevant values as needed and in the proper format

var tempADD = {"domain": tempCookie["domain"],
"expires": tempCookie["expires"],
"expirationDate": 1402418266,
"httpOnly": tempCookie["httpOnly"],
"name": tempCookie["name"],
"path": tempCookie["path"],
"secure": tempCookie["secure"],
"value": tempCookie["value"],
"session": tempCookie["session"],
"storeId": tempCookie["storeId"]};

// Finally, we add the cookie. phantom.addCookie returns true if successful
if(phantom.addCookie(tempADD)){
console.log("Cookie Added!");
} else {
console.log("Cookie Failure: ");
console.log(JSON.stringify(tempADD)) // print the cookie which failed to load
};
}

关于javascript - 无法从 JSON 在 Phantomjs 中添加 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20487028/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com