【问题标题】:Error trying to access firefox cookies using python尝试使用 python 访问 Firefox cookie 时出错
【发布时间】: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”文件有问题。任何其他方式使它工作。伙计们,我真的需要解决这个问题。

【问题讨论】:

    标签: python firefox cookies


    【解决方案1】:

    您的代码或多或少对我有用:

    $ python
    Python 2.7.6 (default, Sep  9 2014, 15:04:36)
    [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from sqlite3 import dbapi2
    >>> con = dbapi2.connect('cookies.sqlite')
    >>> cur = con.cursor()
    >>> cur.execute("SELECT name, value, path, host FROM moz_cookies WHERE host LIKE ?", ["%%www.reddit.com%%"])
    <sqlite3.Cursor object at 0x10bbb3030>
    >>> for item in cur.fetchall():
    ...     print item[0]
    ...
    loid
    loidcreated
    loid
    loidcreated
    

    可能文件已损坏?可以试试直接用sqlite3打开吗?

    $ sqlite3 cookies.sqlite
    SQLite version 3.8.5 2014-08-15 22:37:57
    Enter ".help" for usage hints.
    sqlite> SELECT name FROM moz_cookies WHERE host LIKE "%%www.reddit.com%%";
    loid
    loid
    loidcreated
    loidcreated
    

    【讨论】:

    • 好的...不,不。所以我下载了这个名为“sqlite3.zip”的压缩文件,或者类似的东西。它包含 sqlite3.exe。我在编辑中发布的结果就是这样。否则,如果我在终端上运行命令:“python sqlite3”,它会显示“python:无法打开文件'sqlite3':[Errno 2] 没有这样的文件或目录”。
    • 那么有没有办法,我可以在python程序中使用这个“sqlite3.exe”。我在某处读到,对于 python 2.5 或更高版本,sqlite3 已经存在。你能建议我做什么吗?
    【解决方案2】:

    所以我终于找到了创建“cookies.txt”问题的解决方案。

    注意:我没有损坏“cookies.sqlite”,我的 sqlite3 也没有任何问题。我通过在 python 的解释器中导入来确认它。

    所以我已经运行“cookies.sqlite”并运行“sqlite3”,但是当组合产生“DatabaseError:文件已加密或不是数据库”。按照步骤操作

    • here下载windows sqlite3二进制文件并解压。
    • 打开 cmd 并在提取的目录“sqlite3 cookies.sqlite”中运行。您将拥有 sqlite 终端。在shell中编写以下命令。

      .output cookies.txt # 这会将您的以下命令定向到名为 cookies.txt 的文件

      从 moz_cookies 中选择 *; # 这将从 moz_cookies 中选择所有条目。

    你有你的 cookies.txt 文件。 refered link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-24
      • 2012-07-06
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多