如何在不同的 casperjs 进程之间持久化 cookie [英] how to persist cookies between different casperjs processes

查看:36
本文介绍了如何在不同的 casperjs 进程之间持久化 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于如何将 cookie 从一个 casperjs 页面持久化到另一个页面的问题..

所以基本上我得到了一个 nodejs 文件,它产生 casperjs 作为一个工人来完成某些任务..一个是登录,一旦登录,我将 cookie 存储在一个文件中.

当我生成下一个 casper worker 时.. 我希望它使用 cookie 而不必再次登录.. 这两种方法都失败:

首先: 当我生成 worker capserjs 时,我添加了 --cookies-file=./cookiefilename 即var child = spawn('casperjs',['scrape.js','--cookies-file=./'+cookieFileName]);

第二: 在 casperjs 工作文件中.. 我让它读取并从文件中设置 cookie,即

var casper = require('casper').create();var cookieFileName = 'monsterCookie.txt';//从文件中获取cookiesvar fs = require('fs');var utils = require('utils');var cookies = fs.read(cookieFileName);casper.page.setCookies(cookies);casper.start('domain/page.html', function() {//失败!此处不使用 cookiethis.debugHTML();});casper.run();

注意事项:

  1. 之前提到 start 会从页面中删除 cookie?如果是这样,我该如何防止?
  2. 我知道会话在同一个 phantomjs 页面对象中持续存在(参见此处https://gist.github.com/abbood/5347252),同样的事情发生在同一个 casperjs 页面对象中(见这里 https://gist.github.com/abbood/5347287)
  3. 请记住,我将 cookie 按原样存储在文件中(即根本没有任何 json/cookie 解析).所以我的 cookie 文件看起来完全像这样

<块引用>

[一般]cookies="@Variant(x7fx16QListx1 YCNTR=LB;到期 = 星期二,2013 年 4 月 9 日 17:12:05 GMT;域=.recruiter.domain.com;路径=/qUID=13eb22f-2.21.171.120-1365523938;到期=星期一,2015 年 3 月 30 日 16:12:18 GMT;域=.domain.com;路径=/]UIDR=1365523938;到期=格林威治标准时间 2015 年 3 月 30 日星期一 16:12:18;域=.domain.com;路径=/[R_LANG=en;到期=周四,2013 年 5 月 9 日格林威治标准时间 16:16:06;域=.recruiter.domain.com;path=/x94x43=4gpUmUGr2jgDrs4xOJVrGaNbD8DtYSd1E6quyLhe3E4F3EAGhbRJucnDgRVDeHh0;到期=周四,2013 年 5 月 9 日 16:16:06 GMT;域=.recruiter.domain.com;path=/x94WT_FPC=id=20cf093f17f2c6f3d041365495136954:lv=1365495369854:ss=1365495136954;到期=周五,2023 年 4 月 7 日 08:16:09 GMT;域=.domain.com;path=/xc4x41x43OOKIE=C8ctADE3OC4xMzUuMTQ3LjM5LTI4NzQ5NzQ0LjMwMjkxMjYxAAAAAAAAAAABAamyoBAMo+ZFHhPWRRAQAAAAJWAADKPmQAAAA-U;到期=周四,2015 年 4 月 9 日 16:16:10 GMT;域=statse.domain.com;路径=/Yv1st=CE061E87215F2D73;到期=2020 年 2 月 19 日星期三 14:28:00格林威治标准时间;域=.domain.com;路径=/x84x43OOKIE_ID=178.135.147.39-2368749744.30291261;到期=周五,2023 年 4 月 7 日 16:16:11 GMT;域=cookie.domain.com;路径=/DCS000065_7K5Ixbex41x43OOKIE=C8ctADE3OC4xMzUuMTQ3LjM5LTIzNjg3NDk3NDQuMzAyOTEyNjEAAAAAAAABAAAAQQAAAM0+ZFHNPmNRRAQAAAAE-A到期=周五,2023 年 4 月 7 日 16:16:13 GMT;域=cookie.domain.com;路径=/)"

解决方案

保存 cookie:

var fs = require('fs');var cookies = JSON.stringify(phantom.cookies);fs.write(cookieFilename, cookies, 644);

恢复 cookie:

var fs = require('fs');var data = fs.read(cookieFilename);phantom.cookies = JSON.parse(data);

phantom 是 PhantomJS 中的全局变量.您可以在 wiki

中获取更多信息

this is a question about how to persist cookies from one casperjs page to another..

so basically i got a nodejs file that spawns casperjs as a worker to do certain tasks.. one is to login, once logged in I store the cookie in a file.

when i spawn the next casper worker.. i want it to to use the cookie rather having to login again.. both these methods failed:

first: when i spawn the worker capserjs I add the --cookies-file=./cookiefilename ie var child = spawn('casperjs',['scrape.js','--cookies-file=./'+cookieFileName]);

second: within the casperjs worker file.. I make it read and set the cookie from a file ie

var casper = require('casper').create();
var cookieFileName = 'monsterCookie.txt';

// grab cookies from file 
var fs = require('fs');
var utils = require('utils');
var cookies = fs.read(cookieFileName);

casper.page.setCookies(cookies); 

casper.start('domain/page.html', function() {
    //FAIL! cookies aren't used here
    this.debugHTML();
});

casper.run();

notes:

  1. it was mentioned earlier that start removes cookies from the page? if so how do I prevent that?
  2. I know that sessions persist within the same phantomjs page object (see here https://gist.github.com/abbood/5347252) and same happens within the same casperjs page object (see here https://gist.github.com/abbood/5347287)
  3. keep in mind that I store cookies as is in the file (ie without any json/cookie parsing at all).. so my cookie file looks exactly like this

[General] cookies="@Variant(x7fx16QListx1 YCNTR=LB; expires=Tue, 09-Apr-2013 17:12:05 GMT; domain=.recruiter.domain.com; path=/qUID=13eb22f-2.21.171.120-1365523938; expires=Mon, 30-Mar-2015 16:12:18 GMT; domain=.domain.com; path=/]UIDR=1365523938; expires=Mon, 30-Mar-2015 16:12:18 GMT; domain=.domain.com; path=/[R_LANG=en; expires=Thu, 09-May-2013 16:16:06 GMT; domain=.recruiter.domain.com; path=/x94x43=4gpUmUGr2jgDrs4xOJVrGaNbD8DtYSd1E6quyLhe3E4F3EAGhbRJucnDgRVDeHh0; expires=Thu, 09-May-2013 16:16:06 GMT; domain=.recruiter.domain.com; path=/x94WT_FPC=id=20cf093f17f2c6f3d041365495136954:lv=1365495369854:ss=1365495136954; expires=Fri, 07-Apr-2023 08:16:09 GMT; domain=.domain.com; path=/xc4x41x43OOKIE=C8ctADE3OC4xMzUuMTQ3LjM5LTI4NzQ5NzQ0LjMwMjkxMjYxAAAAAAAAAAABAAAAmyoBAMo+ZFHhPWRRAQAAAAJWAADKPmRR4T1kUQAAAAA-; expires=Thu, 09-Apr-2015 16:16:10 GMT; domain=statse.domain.com; path=/Yv1st=CE061E87215F2D73; expires=Wed, 19-Feb-2020 14:28:00 GMT; domain=.domain.com; path=/x84x43OOKIE_ID=178.135.147.39-2368749744.30291261; expires=Fri, 07-Apr-2023 16:16:11 GMT; domain=cookie.domain.com; path=/DCS000065_7K5Ixbex41x43OOKIE=C8ctADE3OC4xMzUuMTQ3LjM5LTIzNjg3NDk3NDQuMzAyOTEyNjEAAAAAAAABAAAAQQAAAM0+ZFHNPmRRAQAAAAEAAADNPmRRzT5kUQAAAAA-; expires=Fri, 07-Apr-2023 16:16:13 GMT; domain=cookie.domain.com; path=/)"

解决方案

Saving cookies:

var fs = require('fs');
var cookies = JSON.stringify(phantom.cookies);
fs.write(cookieFilename, cookies, 644);

Restoring cookies:

var fs = require('fs');
var data = fs.read(cookieFilename);
phantom.cookies = JSON.parse(data);

The phantom is global variable in PhantomJS. More information you can get in wiki

这篇关于如何在不同的 casperjs 进程之间持久化 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆