ruby 编码读取FireFox的cookies.
上一篇 /
下一篇 2009-07-09 12:33:03
/ 个人分类:所遇问题
这几天遇到一部分case需要自动读取FireFox环境下的cookies,一统google之后,终于找到了解决办法:
先说cookies,FireFox的cookies为了提高效率是用sqlite数据库管理的,因此编码的话,只能对此数据库进行操作。
第一步:安装sqlite的驱动:gem install sqlite3-ruby --version1.2.3
第二步:分析cookies表结构:从网上搜索了一个sqlite的小工具:sharpPlus Sqlite Developer,moz_cookies表结构为:
Begin Transaction;
Create TABLE MAIN.[Temp_695990402](
[id] INTEGER PRIMARY KEY
,[name] TEXT
,[value] TEXT
,[host] TEXT
,[path] TEXT
,[expiry] INTEGER
,[lastAccessed] INTEGER
,[isSecure] INTEGER
,[isHttpOnly] INTEGER
);
Insert Into MAIN.[Temp_695990402] ([id],[name],[value],[host],[path],[expiry],[lastAccessed],[isSecure],[isHttpOnly])
Select [id],[name],[value],[host],[path],[expiry],[lastAccessed],[isSecure],[isHttpOnly] From MAIN.[moz_cookies];
Drop Table MAIN.[moz_cookies];
Alter Table MAIN.[Temp_695990402] Rename To [moz_cookies];
Commit Transaction;
第三步:读取需要的数据:
require 'sqlite3'
程序段如下:
#open the cookies database.
dir_firefox = "C:\\Documents and Settings\\User\\Application Data\\Mozilla\\Firefox\\Profiles\\gqor69hv.default"
db = SQLite3::Database.open( dir_firefox + "\\cookies.sqlite")
#try to get the number of the cookies which host is .
real_count = db.get_first_value("SELECT count(host) FROM moz_cookies WHERE host LIKE '%#{所需要的内容}%'" )
assert(real_count.to_i>0);
db.execute("delete from moz_cookies where host like '%store.int.real%'")
db.close()
收藏
举报
TAG: