【发布时间】:2015-10-07 05:37:26
【问题描述】:
您好,我正在研究在 python 中使用 firefox cookie。我推荐了this。我收到“DatabaseError:文件已加密或不是数据库”错误。我也经历了其他答案,他们似乎暗示了兼容性问题,但没有一个详细说明如何解决这个问题。
操作系统:Windows 7,Firefox 版本:38,Python:2.7.9。
请帮我解决这个问题。 下面是代码。顺便说一句,我的工作目录中有“cookies.sqlite”的副本,所以不要费心回答我的路径不正确。
谢谢。
import urllib2
import cookielib
from sqlite3 import dbapi2
host = 'www.reddit.com'
ff_cookie_file= 'cookies.sqlite'
file = open("cookie.txt", "w")
file.write("#LWP-Cookies-2.0\n")
match = '%%%s%%' % host
con = dbapi2.connect(ff_cookie_file)
cur = con.cursor()
cur.execute("select name, value, path, host from moz_cookies where host like ?", [match])
for item in cur.fetchall():
cookie = "Set-Cookie3: %s=\"%s\"; path=\"%s\"; \
domain=\"%s\"; expires=\"2038-01-01 00:00:00Z\"; version=0\n" % (item[0], item[1], item[2], item[3])
file.write(cookie)
file.close()
cj = cookielib.LWPCookieJar()
cj.load("cookie.txt")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
编辑:运行命令“sqlite3 cookies.sqlite”给出以下输出:
python: can't open file 'sqlite3': [Errno 2] No such file or directory
我猜这是因为我的系统没有“sqlite3.exe”文件。
为了进一步确认我的 sqlite3 是否有效,我创建了数据库,插入了一些值并进行了查询。它就像魅力一样。 here is the link。所以我猜“cookies.sqlite”文件有问题。任何其他方式使它工作。伙计们,我真的需要解决这个问题。
【问题讨论】: