user3206440
user3206440

Reputation: 5049

phantomjs - store and send cookies

I'm using PhantomJS as follows to retrieve and parse data from a website.

const phantom = require('phantom');
const url = 'https://stackoverflow.com/'

let _ph, _page, _outObj;
phantom.create()
.then( (ph) => {
    _ph = ph;
    return _ph.createPage();
}).then( (page) => {
    _page = page;
    return page.open(url);
}).then( (status) => {
    console.log(`Status: ${status}`);
    return _page.property('content');
}).then( (data) => {
    console.log(data);
    _page.close();
    _ph.exit();
}).catch( (e) => console.log(e));

What I need to do also is to store the cookie send by the server and include them in subsequent requests to the server - how do I go about it ?

Upvotes: 0

Views: 252

Answers (1)

Vaviloff
Vaviloff

Reputation: 16838

PhantomJS is capable of storing and loading cookies by itself, according to docs there is a cli option for that:

--cookies-file=/path/to/cookies.txt specifies the file name to store the persistent Cookies

So with phantom node module you pass this option upon browser creation:

phantom.create(['--cookies-file=/path/to/cookies.txt']).then(...)

Upvotes: 3

Related Questions