如何将Firefox Cookie导入python请求 [英] How to import firefox cookies to python requests

查看:128
本文介绍了如何将Firefox Cookie导入python请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已登录 Firefox 的某个页面,并且希望使用 cookie 并尝试使用 python-requests 浏览网页.问题是,在将cookie导入请求会话之后,什么也没有发生(就像根本没有cookie).由请求发出的Cookie的结构也与Firefox中的不同. 这样可以加载FF cookie并在请求会话中使用它吗?

I'm logged in on some page in Firefox and I want to take the cookie and try to browse webpage with python-requests. Problem is that after importing cookie to the requests session nothing happen (like there is no cookie at all). Structure of the cookie made by requests differ from the one from Firefox as well. Is such it possible to load FF cookie and use it in requests session?

到目前为止,我的代码:

My code so far:

import sys
import sqlite3
import http.cookiejar as cookielib
import requests
from requests.utils import dict_from_cookiejar

def get_cookies(final_cookie, firefox_cookies):
    con = sqlite3.connect(firefox_cookies)
    cur = con.cursor()
    cur.execute("SELECT host, path, isSecure, expiry, name, value FROM moz_cookies")
    for item in cur.fetchall():
        if item[0].find("mydomain.com") == -1:
            continue
        c = cookielib.Cookie(0, item[4], item[5],
            None, False,
            item[0], item[0].startswith('.'), item[0].startswith('.'),
            item[1], False,
            item[2],
            item[3], item[3]=="",
            None, None, {})
        final_cookie.set_cookie(c)



cookie = cookielib.CookieJar()
input_file = ~/.mozilla/firefox/myprofile.default/cookies.sqlite
get_cookies(cookie, input_file)

#print cookie given from firefox
cookies = dict_from_cookiejar(cookie)
for key, value in cookies.items():
    print(key, value)


s = requests.Session()
payload = {
"lang" : "en",
'destination': '/auth',
'credential_0': sys.argv[1],
'credential_1': sys.argv[2],
'credential_2': '86400',
}
r = s.get("mydomain.com/login", data = payload)
#print cookie from requests
cookies = dict_from_cookiejar(s.cookies)
for key, value in cookies.items():
    print(key, value)

firefox中cookie的结构是:

Structure of cookies from firefox is:

_gid GA1.3.2145214.241324
_ga GA1.3.125598754.422212
_gat_is4u 1

请求中Cookie的结构是:

Structure of cookies from requests is:

UISTestAuth tesskMpA8JJ23V43a%2FoFtdesrtsszpw

毕竟,当尝试将FF中的cookie分配给session.cookies时,请求有效,因为我什么都不导入.

After all, when trying to assign cookies from FF to session.cookies, requests works as I import nothing.

推荐答案

Firefox中似乎有两种Cookie:请求响应.可以在页面检查器>网络>登录(发布)> Cookies 时看到:

It looks like there are two type of cookies in the Firefox - request and response. It could be seen while Page inspector > Network > login (post) > Cookies:

Response cookies:   
    UISAuth 
        httpOnly   true
        path       /
        secure     true
        value      tesskMpA8JJ23V43a%2FoFtdesrtsszpw
Request cookies:    
    _ga            GA1.3.125598754.422212
    _gat_is4u      1
    _gid           GA1.3.2145214.241324

请求 cookie存储在

The request cookies are stored in the cookies.sqlite file in the

~/.mozilla/firefox/*.default/cookies.sqlite

并可以通过更多方式加载到python对象,例如:

and can be load to the python object in more ways, for example:

import sqlite3
import http.cookiejar

def get_cookies(cj, ff_cookies):
    con = sqlite3.connect(ff_cookies)
    cur = con.cursor()
    cur.execute("SELECT host, path, isSecure, expiry, name, value FROM moz_cookies")
    for item in cur.fetchall():
        c = cookielib.Cookie(0, item[4], item[5],
            None, False,
            item[0], item[0].startswith('.'), item[0].startswith('.'),
            item[1], False,
            item[2],
            item[3], item[3]=="",
            None, None, {})
        print c
        cj.set_cookie(c)

其中cj是CookieJar对象,而ff_cookies是Firefox cookies.sqlite 的路径.取自此页面

where cj is CookieJar object and ff_cookies is path to Firefox cookies.sqlite. Taken from this page.

使用会话加载cookie并导入到python请求的整个代码如下:

The whole code to load cookies and import to the python requests using session would looks like:

 import requests
 import sys

 cj = http.cookiejar.CookieJar()
 ff_cookies = sys.argv[1] #pass path to the cookies.sqlite as an argument to the script
 get_cookies(cj, ff_cookies)
 s = requests.Session()
 s.cookies = cj

响应基本上是会话ID,通常会在会话结束(或某些超时)后过期,因此不会存储.

Response cookie is basically session ID, which usualy expires at the end of the session (or some timeout), so they are not stored.

这篇关于如何将Firefox Cookie导入python请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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