Javascript 跨噩梦ejs实例持久化cookie
我如何在多个噩梦ejs实例中持久化并传递cookie?Javascript 跨噩梦ejs实例持久化cookie,javascript,node.js,web-scraping,nightmare,Javascript,Node.js,Web Scraping,Nightmare,我如何在多个噩梦ejs实例中持久化并传递cookie? 任何示例代码都会很有帮助。下面是一个示例,说明如何使用以前会话中保存的一些cookie设置会话 // create a promise for retrieving cookies function getCookies(url) { return new Promise(function (resolve) { Nightmare() .goto(url) .cookies.get() // get t
任何示例代码都会很有帮助。下面是一个示例,说明如何使用以前会话中保存的一些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,必须循环对吗?它会起作用,因为COOKIES.set(cookiesArray)可以接受cookie对象的数组。