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

问题描述 投票:0回答:1

我从网站上尝试存储cookie。但是,当我创建一个全局变量并尝试将其存储在变量上时,它返回为未定义或当我尝试创建一个带有cookie的文件时,它返回undefined as好吧,但我可以用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://stackoverflow.com').then(function(status) {
  if(status == 'success') {
     console.log('success');
  }  
});

});
});

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

node.js phantomjs phantomjs-node
1个回答
0
投票

您可以查看可能的用例的测试。这是一个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),
});
© www.soinside.com 2019 - 2024. All rights reserved.