Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 跨噩梦ejs实例持久化cookie_Javascript_Node.js_Web Scraping_Nightmare - Fatal编程技术网

Javascript 跨噩梦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

我如何在多个噩梦ejs实例中持久化并传递cookie?
任何示例代码都会很有帮助。

下面是一个示例,说明如何使用以前会话中保存的一些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对象的数组。