我通过响应(通过GET请求)从jar中提取了一些cookie,并且想知道是否可以将它们转换为JSON,并在puppeteer中使用它们以将这些cookie加载到页面中。
尝试:
cookieJar的格式:
jar: RequestJar {
_jar: CookieJar {
enableLooseMode: true,
store: { idx: {
'example.com': {
'/': {
frontend: Cookie="frontend=n7hnfv8971ai4lkcgvqs1m500s; Expires=Sun, 30 Jun 2019 11:51:59 GMT; Max-Age=86400; Domain=wellgosh.com; Path=/; HttpOnly; hostOnly=false; aAge=18ms; cAge=16864ms",
frontend_cid: Cookie="frontend_cid=fGjhTirhLEc2XovB; Expires=Sun, 30 Jun 2019 11:51:59 GMT; Max-Age=86400; Domain=wellgosh.com; Path=/; Secure; HttpOnly; hostOnly=false; aAge=23ms; cAge=16860ms",
nocache: Cookie="nocache=1; Expires=Sun, 30 Jun 2019 11:51:59 GMT; Max-Age=86400; Domain=wellgosh.com; Path=/; HttpOnly; hostOnly=false; aAge=16ms; cAge=16855ms",
external_no_cache: Cookie="external_no_cache=1; Expires=Sat, 29 Jun 2019 12:51:59 GMT; Max-Age=3600; Domain=wellgosh.com; Path=/; HttpOnly; hostOnly=false; aAge=20ms; cAge=1198ms"
}
}
} }
}
}
function logCookies(cookieJar){
cookieJar._jar.store.getAllCookies(function(err, cookieArray) {
if(err) throw new Error("Failed to get cookies");
let format = (JSON.stringify(cookieArray, null, 4));
// let format = (cookieArray.map(cookie => cookie.toString()))
console.log(format)
pupLaunch(format)
});
async function pupLaunch(format) {
const browser = await puppeteer.launch({
headless: false,
//args: ['--proxy-server=' + proxy]
})
const page = await browser.newPage()
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36');
await page.goto(url);
for(x in cookieArray.length){
await page.setCookie(JSON.parse(format)[x])
}
}
但是,该页面加载时没有正确导入cookie。
如何转换jar,使其在puppeteer中工作?
答案 0 :(得分:0)
在导航之前使用Cookie
const page = await browser.newPage();
await page.setCookie(...cookies);
await page.goto(URL);