如何使用Python从Web浏览器中获取Cookie? [英] How to get cookies from web-browser with Python?
问题描述
上下文:
我正在研究对OpenID使用者(实际上是StackExchange)的后端访问.如果要向用户提供所有可能的OpenID提供程序作为选项,那么在提交Open ID URL之前,我必须模拟浏览器交互以对这些提供程序进行身份验证.但是,我认为可以通过访问用户网络浏览器的现有cookie,并直接使用URL向消费者请求身份验证来缩短这一时间.
问题:
如何访问用户的网络浏览器的cookie?我很少看到有关如何使用Python的信息.这个上一个问题部分回答了有关Firefox的问题,在下面特别指出代码示例.但是,我需要从Linux上使用的最常见的Web浏览器访问cookie,而不仅仅是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
问题:Python是否提供了一个有助于从网络浏览器中提取Cookie的模块?否则,我应该如何修改以上代码以从其他浏览器(例如Chromium等)中提取Cookie? >
PS:还是我看错了最初的问题(即向OpenID提供程序进行身份验证)? (我觉得我只是在用另一个问题代替.)
我创建了一个模块来完全做到这一点,可以在这里找到: 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\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)
python3 fork: https://github.com/borisbabic/browser_cookie3
这篇关于如何使用Python从Web浏览器中获取Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!