2
我將如何堅持並將cookie傳遞給多個NightmareJS實例? 任何示例代碼都會有幫助。在NightmareJS實例中存在餅乾
我將如何堅持並將cookie傳遞給多個NightmareJS實例? 任何示例代碼都會有幫助。在NightmareJS實例中存在餅乾
下面是一個如何設置會話的示例,其中包含一些從早期會話保存的cookie。
// create a promise for retrieving cookies
function getCookies(url) {
return new Promise(function (resolve) {
Nightmare()
.goto(url)
.cookies.get() // get the cookies
.end()
.then(resolve)
});
}
var COOKIES;
getCookies(url).then(function (cookies) {
// save your cookies somewhere...
// you could save them in the file system (with NodeJs)
require('fs').writeFileSync(
'cookies.json',
JSON.stringify(cookies)
);
// or you could set a global variable
COOKIES = cookies;
})
// and now each time you want to use these cookies
// you would simply set them before each session
function google() {
return new Promise(function (resolve) {
Nightmare()
.goto('about:blank') // create your session by going to a blank page
.cookies.set(COOKIES) // or .cookies.set(require('fs').readFileSync('cookies.json'))
.goto('http://google.com')
// do your thing with the new cookies..
.end()
.then(resolve)
})
}
'cookie.set(COOKIES)'不起作用我想因爲它的一個對象或cookie而不是一個cookie,必須循環對嗎? – 3zzy