如何使用节点幻影存储 cookie?

how can I store cookies with node phantom?

我试图在从 website.But 获取后存储 cookie,当我创建一个全局变量并尝试将其存储在 return 未定义的变量上时,或者当我尝试创建一个带有 cookie 的文件,它 return 也未定义,但我可以用 console.log.

打印它

我的代码:

var cookies;

phantom.create(['--ignore-ssl-errors=yes', '--load-images=no', '--ssl-
protocol=any']).then(function(ph) {
ph.createPage().then(function(page) {

page.property("onConsoleMessage", function(message) {
   console.log(message); 
});

page.property('onResourceReceived', function(response) {
        console.log(phantom.cookies); //Show the page cookies
        cookies = phantom.cookies;
       fs.write(CookieJar, JSON.stringify(phantom.cookies), "w"); //its not creating a file
}).then(function() {
      console.log('Cookies');
      console.log(phantom.cookies); //Equals to undefined
      console.log(cookies); //Equals to undefined
});;

page.open('https://whosebug.com').then(function(status) {
  if(status == 'success') {
     console.log('success');
  }  
});

});
});

如何将这些 cookie 存储在文件中并获取它?

您可以查看可能用例的测试。这是一个example

await page.addCookie({
  name: 'Valid-Cookie-Name',
  value: 'Valid-Cookie-Value',
  domain: 'localhost',
  path: '/foo',
  httponly: true,
  secure: false,
  expires: new Date().getTime() + (1000 * 60 * 60),
});