在 Python 中访问 Firefox 3 cookie

发布于 2024-10-10 13:30:49 字数 317 浏览 5 评论 0原文

我正在尝试制作一个 python 脚本,该脚本将在 Firefox 中使用 cookie 访问网站。如果 cookielib.MozillaCookieJar 支持 Firefox 3,它就会工作。有没有办法在 python 中访问 Firefox 3 cookie?

我看到 [home]/.mozilla/firefox/[randomletters].default/ 下有两个文件,名为 cookies.sqlite 和 cookies-nontor.xml。 .xml 文件看起来很容易编写一个从中返回 CookieJar 的函数,但如果已经有一个模块可以执行此操作,那么我想避免重新发明轮子。

I'm trying to make a python script that will access a website with the cookies in Firefox. cookielib.MozillaCookieJar would work if it supported Firefox 3. Is there a way to access Firefox 3 cookies inside of python?

I see that there are two files under [home]/.mozilla/firefox/[randomletters].default/ called cookies.sqlite and cookies-nontor.xml. The .xml file looks like it would be easy to write a function that will return a CookieJar from it, but if there's already a module that does this, then I'd like to avoid re-inventing the wheel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情哥ㄟ 2024-10-17 13:30:49

这是访问 FF 3 中 SQLite cookie 的秘方 Python bug TrackerMechanize 也支持这一点。

Here's a recipe for accessing the SQLite cookies in FF 3. There's a patch at the Python bug Tracker and Mechanize also supports this.

撩起发的微风 2024-10-17 13:30:49

我创建了一个从 Firefox 加载 cookie 的模块,可在此处找到: https://bitbucket.org/richardpenman/browser_cookie/< /a>

用法示例:

import requests
import browser_cookie
cj = browser_cookie.firefox()
r = requests.get(url, cookies=cj)

I created a module to load cookies from Firefox, available here: https://bitbucket.org/richardpenman/browser_cookie/

Example usage:

import requests
import browser_cookie
cj = browser_cookie.firefox()
r = requests.get(url, cookies=cj)
往昔成烟 2024-10-17 13:30:49

TryPyPy 的答案 让我走上了正轨,但链接配方中的代码已经过时,无法在 Python3 上运行。以下是 Python3 代码,它将从正在运行的 Firefox 读取 cookie jar 并在查询网页时使用它:

import requests

url = 'http://github.com'
cookie_file = '/home/user/.mozilla/firefox/f00b4r.default/cookies.sqlite'



def get_cookie_jar(filename):
    """
    Protocol implementation for handling gsocmentors.com transactions
    Author: Noah Fontes nfontes AT cynigram DOT com
    License: MIT
    Original: http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python

    Ported to Python 3 by Dotan Cohen
    """

    from io import StringIO
    import http.cookiejar
    import sqlite3

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

    ftstr = ["FALSE","TRUE"]

    s = StringIO()
    s.write("""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
""")

    for item in cur.fetchall():
        s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (
            item[0], ftstr[item[0].startswith('.')], item[1],
            ftstr[item[2]], item[3], item[4], item[5]))

    s.seek(0)
    cookie_jar = http.cookiejar.MozillaCookieJar()
    cookie_jar._really_load(s, '', True, True)

    return cookie_jar



cj = get_cookie_jar(cookie_file)
response = requests.get(url, cookies=cj)
print(response.text)

在带有 Python 3.4.2 和 Firefox 39.0 的 Kubuntu Linux 14.10 上测试。该代码也可从我的 Github 存储库获取。

TryPyPy's answer got me on the right track, but the code in the linked recipe was outdated and would not work with Python3. Here is Python3 code that will read the cookie jar from a running Firefox and use it when querying web pages:

import requests

url = 'http://github.com'
cookie_file = '/home/user/.mozilla/firefox/f00b4r.default/cookies.sqlite'



def get_cookie_jar(filename):
    """
    Protocol implementation for handling gsocmentors.com transactions
    Author: Noah Fontes nfontes AT cynigram DOT com
    License: MIT
    Original: http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python

    Ported to Python 3 by Dotan Cohen
    """

    from io import StringIO
    import http.cookiejar
    import sqlite3

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

    ftstr = ["FALSE","TRUE"]

    s = StringIO()
    s.write("""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
""")

    for item in cur.fetchall():
        s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (
            item[0], ftstr[item[0].startswith('.')], item[1],
            ftstr[item[2]], item[3], item[4], item[5]))

    s.seek(0)
    cookie_jar = http.cookiejar.MozillaCookieJar()
    cookie_jar._really_load(s, '', True, True)

    return cookie_jar



cj = get_cookie_jar(cookie_file)
response = requests.get(url, cookies=cj)
print(response.text)

Tested on Kubuntu Linux 14.10 with Python 3.4.2 and Firefox 39.0. The code is also available from my Github repo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文