一半君的总结纸

听话只听一半君

用python开firefox 27的cookies.sqlite拿cookie

试了半天 sqlite3 module死活不行,难道是因为 他的版本太低?

错误提示

>>> import sqlite3
>>> con = sqlite3.connect(cookiesFile)
>>> cur = con.cursor()
>>> cur.execute("SELECT host, path, isSecure, expiry, name, value FROM moz_cookies")

Traceback (most recent call last):
  File "<pyshell#189>", line 1, in <module>
    cur.execute("SELECT host, path, isSecure, expiry, name, value FROM moz_cookies")
DatabaseError: file is encrypted or is not a database

不知道这个版本算低么,但是他打不开firefox的cookies.sqlite 但是在sqlite官网下的命令行工具是可以开这个文件的,版本3.8.3.1

>>> sqlite3.sqlite_version
'3.6.21'

搜到有人说只能重新compile python?!WTF … 完全超出能力范围之外,后又搜索到可以试试 pysqlite, 结果pip安装的时候提示类似这样的..

...
File "C:\Python27\lib\distutils\msvc9compiler.py", line 299, in query_vcvarsall
raise ValueError(str(list(result.keys())))
ValueError: [u'path']

根据各位Stackoverflow大神的指南脑残解决法1,去下载了visual studio 2008 professional, 在装vc++ 2008的时候勾上了x64 compilers and tools.
再次尝试,结果出现新错误提示

...
pysqlite \connection.h(33) : fatal error C1083: Cannot open include file: 'sqlite3.h': No such file or directory

遂采取一贯的乱搞法,去sqlite官网下载source,把此文件放到了c:\python27\include下
之后又有新错误,我擦

...
LINK : fatal error LNK1181: cannot open input file 'sqlite3.lib'
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe"' failed with exit status 1181

这次继续乱搞,打开Visual Studio Command Prompt,在sqlite source解开的文件夹里运行

lib /def:sqlite3.def /out:sqlite3.lib

然后把这个文件复制到 c:\python27\libs,这次终于把pysqlite给安上了,马上测试,依旧不行,我了个大擦!

继续乱搜后得知,必须如下操作,换句话说上面的都白搞了(不过lz也不能确定)
下载pysqlite source之后

python setup.py build_static
python setup.py install

终于搞成最新版了

>>> from pysqlite2 import dbapi2 as sqlite
>>> sqlite.sqlite_version
'3.8.3.1'

再次试验

>>> con = sqlite.connect(cookiesFile)
>>> cur = con.cursor()
>>> cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies")
<pysqlite2.dbapi2.Cursor object at 0x0000000002F3CA40>

我了个去… 终于成功了….

Advertisements

2 responses to “用python开firefox 27的cookies.sqlite拿cookie

  1. andy 四月 19, 2014 @ 10:59 上午

    帅哥我python setup.py build时,提示sqlite3.h Nofile or Dir.我看你也是window是,你是如何解决的?

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: