如何使用 Python 从网络浏览器获取 cookie? [英] How to get cookies from web-browser with Python?

查看:36
本文介绍了如何使用 Python 从网络浏览器获取 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:
我正在处理对 OpenID 使用者(实际上是 StackExchange)的后端访问.如果我要向用户提供所有可能的 OpenID 提供程序作为一个选项,那么在我提交 Open ID URL 之前,我必须模拟浏览器交互以对这些提供程序中的每一个进行身份验证.但是,我认为我可以通过访问用户的网络浏览器的现有 cookie,并直接使用 URL 向消费者请求身份验证来缩短这个时间.

问题:
如何访问用户的网络浏览器的 cookie?我很少看到有关如何使用 Python 执行此操作的信息.这个上一个问题部分回答了有关 Firefox 的问题,特别指出下面的代码示例.但是,我需要从 Linux 上使用的最常用网络浏览器访问 cookie,而不仅仅是 Firefox.

#!/usr/bin/env python# 处理 gsocmentors.com 交易的协议实现#作者:Noah Fontes nfontes AT cynigram DOT com# 许可证:麻省理工学院def sqlite2cookie(文件名):从 cStringIO 导入 StringIO从 pysqlite2 导入 dbapi2 作为 sqlitecon = sqlite.connect(文件名)cur = con.cursor()cur.execute("选择主机、路径、isSecure、过期、名称、来自 moz_cookies 的值")ftstr = [假",真"]s = StringIO()s.write("""# Netscape HTTP Cookie 文件# http://www.netscape.com/newsref/std/cookie_spec.html# 这是一个生成的文件!不要编辑.""")对于 cur.fetchall() 中的项目:s.write("%s	%s	%s	%s	%s	%s	%s
" % (item[0], ftstr[item[0].startswith('.')], item[1],ftstr[item[2]], item[3], item[4], item[5]))s.seek(0)cookie_jar = cookielib.MozillaCookieJar()cookie_jar._really_load(s, '', True, True)返回 cookie_jar

问题:Python 是否提供了一个模块可以促进从网络浏览器中提取 cookie?否则,我应该如何调整上述代码以从其他浏览器(如 Chromium 等)中提取 cookie?>

PS:或者我是不是以错误的方式看待最初的问题(即向 OpenID 提供者进行身份验证)?(我觉得我只是用另一个问题代替了一个问题.)

解决方案

我创建了一个模块来做到这一点,可在此处获得:https://bitbucket.org/richardpenman/browsercookie/

示例用法:

导入请求导入浏览器cookiecj = browsercookie.chrome()r = requests.get('http://stackoverflow.com', cookies=cj)

python3 fork: https://github.com/borisbabic/browser_cookie3

Context:
I am working on a backend access to an OpenID consumer (StackExchange in fact). If I am to provide all possible OpenID providers as an option to the user, then I'd have to simulate browser interaction to authenticate to each of these providers before I could submit the Open ID URL. However, I think I could cut this short by accessing the existing cookies of the user's web-browser, and requesting authentication to the consumer directly with the URL.

Problem:
How to access the user's web-browser's cookies? I've seen very little information on how to do it with Python. This previous question partly answers the problem regarding Firefox, pointing especially to the code sample her below. However, I would need to access cookies from the most common web browsers used on Linux, not just Firefox.

#! /usr/bin/env python
# Protocol implementation for handling gsocmentors.com transactions
# Author: Noah Fontes nfontes AT cynigram DOT com
# License: MIT

def sqlite2cookie(filename):
    from cStringIO import StringIO
    from pysqlite2 import dbapi2 as sqlite

    con = sqlite.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	%s	%s	%s	%s	%s	%s
" % (
            item[0], ftstr[item[0].startswith('.')], item[1],
            ftstr[item[2]], item[3], item[4], item[5]))

    s.seek(0)

    cookie_jar = cookielib.MozillaCookieJar()
    cookie_jar._really_load(s, '', True, True)
    return cookie_jar

Question: Does Python provide a module that can facilitate cookie extraction from web-browsers? Otherwise, how should I adapt the above code to draw cookies from other browsers, like Chromium etc.?

PS: Or am I looking at the initial problem (i.e. authenticate to the OpenID provider) the wrong way? (I feel I am just replacing a problem by another.)

解决方案

I created a module to do exactly that, available here: https://bitbucket.org/richardpenman/browsercookie/

Example usage:

import requests
import browsercookie
cj = browsercookie.chrome()
r = requests.get('http://stackoverflow.com', cookies=cj)

python3 fork: https://github.com/borisbabic/browser_cookie3

这篇关于如何使用 Python 从网络浏览器获取 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆