2017-10-19 1 views
0

私はウェブサイトから取得した後にクッキーを保存しようとしています。しかし、グローバル変数を作成して変数に保存しようとすると、未定義として返されるか、またはクッキーを含むファイルを作成しようとすると、未定義ですが、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'); 
    } 
}); 

}); 
}); 

がどのように私は、ファイルにこれらのクッキーを保存し、それを得ることができますか?

答えて

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), 
}); 
関連する問題