python 打开网页获取cookies,如何使用Python从Web浏览器中获取Cookie?

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\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 = 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值