28

我正在尝试使用 python requests 1.2.3 库将 cookie 添加到现有的 cookiejar 中。每次我添加新的 cookie 时,jar 中的数据都会被用于新的 cookie。缺少键、缺少值或与不正确的键匹配。我不确定这是请求库错误还是我没有正确发送 cookie。我正在使用以下代码导致 cookiejar 中的 cookie 错误。我是否正确格式化了 cookie?有任何想法吗?

    my_cookie = {
           'domain':'www.mydomain.com',
           'expires':None,
           'name':'COOKIE_NAME',
           'path':'/',
           'value':'the cookie works',
           'version':0
}

s = requests.Session()
requests.utils.add_dict_to_cookiejar(s.cookies, my_cookie)
4

4 回答 4

45

快速回答

选项1

import requests
s = requests.session()
s.cookies.set("COOKIE_NAME", "the cookie works", domain="example.com")

选项 2

import requests
s = requests.session()
# Note that domain keyword parameter is the only optional parameter here
cookie_obj = requests.cookies.create_cookie(domain="example.com",name="COOKIE_NAME",value="the cookie works")
s.cookies.set_cookie(cookie_obj)

详细解答

我不知道在询问原始问题时这种技术是否有效,但理想情况下,您将使用生成自己的 cookie 对象requests.cookies.create_cookie(name,value,**kwargs),然后通过requests.cookies.RequestsCookieJar.set_cookie(cookie,*args,**kwargs). 请参阅此处的源/文档。

向请求会话添加自定义 cookie

>>> import requests
>>> s = requests.session()
>>> print(s.cookies)
<RequestsCookieJar[]>
>>> required_args = {
        'name':'COOKIE_NAME',
        'value':'the cookie works'
    }
>>> optional_args = {
    'version':0,
    'port':None,
#NOTE: If domain is a blank string or not supplied this creates a
# "super cookie" that is supplied to all domains.
    'domain':'example.com',
    'path':'/',
    'secure':False,
    'expires':None,
    'discard':True,
    'comment':None,
    'comment_url':None,
    'rest':{'HttpOnly': None},
    'rfc2109':False
}
>>> my_cookie = requests.cookies.create_cookie(**required_args,**optional_args)
# Counter-intuitively, set_cookie _adds_ the cookie to your session object,
#  keeping existing cookies in place
>>> s.cookies.set_cookie(my_cookie)
>>> s.cookies
<RequestsCookieJar[Cookie(version=0, name='COOKIE_NAME', value='the cookie works', port=None, port_specified=False, domain='www.domain.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>

奖励:让我们添加一个超级 cookie,然后将其删除

>>> my_super_cookie = requests.cookies.create_cookie('super','cookie')
>>> s.cookies.set_cookie(my_super_cookie)
# Note we have both our previous cookie and our new cookie
>>> s.cookies
<RequestsCookieJar[Cookie(version=0, name='super', value='cookie', port=None, port_specified=False, domain='', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='COOKIE_NAME', value='the cookie works', port=None, port_specified=False, domain='www.domain.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>
# Deleting is simple, note that this deletes the cookie based on the name,
# if you have multiple cookies with the same name it will raise
# requests.cookies.CookieConflictError
>>> del s.cookies['super']
>>> s.cookies
<RequestsCookieJar[Cookie(version=0, name='COOKIE_NAME', value='the cookie works', port=None, port_specified=False, domain='www.domain.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>
于 2018-08-18T01:41:06.853 回答
20

我通过导入 CookieJar、Cookie 和 cookie 找到了一种方法。在@Lukasa 的帮助下,他向我展示了一个更好的方法。但是,以他的方式,我无法指定“port_specified”、“domain_specified”、“domain_initial_dot”或“path_specified”属性。“set”方法使用默认值自动执行。我正在尝试抓取一个网站,他们的 cookie 在这些属性中具有不同的值。由于我对这一切都不熟悉,所以我不确定这是否真的很重要。

my_cookie = {
"version":0,
"name":'COOKIE_NAME',
"value":'true',
"port":None,
# "port_specified":False,
"domain":'www.mydomain.com',
# "domain_specified":False,
# "domain_initial_dot":False,
"path":'/',
# "path_specified":True,
"secure":False,
"expires":None,
"discard":True,
"comment":None,
"comment_url":None,
"rest":{},
"rfc2109":False
}

s = requests.Session()
s.cookies.set(**my_cookie)
于 2013-06-21T16:59:35.710 回答
15

如果您要处理普通的 cookie str:

def make_cookiejar_dict(cookies_str):
    # alt: `return dict(cookie.strip().split("=", maxsplit=1) for cookie in cookies_str.split(";"))`
    cookiejar_dict = {}
    for cookie_string in cookies_str.split(";"):
        # maxsplit=1 because cookie value may have "="
        cookie_key, cookie_value = cookie_string.strip().split("=", maxsplit=1)
        cookiejar_dict[cookie_key] = cookie_value
    return cookiejar_dict


cookies_str = '''nopubuser_abo=1; groupenctype_abo="1="'''
cj = requests.utils.cookiejar_from_dict(make_cookiejar_dict(cookies_str))
sess = requests.Session()
sess.cookies = cj
于 2016-05-14T17:08:25.627 回答
9

要使用内置函数和方法...

import requests

session = requests.session()
my_cookies = {'cookie_key': 'cookie_value',
              'another_cookie_key': 'another_cookie_value'}
requests.utils.add_dict_to_cookiejar(session.cookies, my_cookies)

您可以根据需要添加任意数量的 cookie。如果您需要特殊标题,请使用此方法添加它们。

my_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'}
session.headers.update(my_headers)
于 2020-05-21T22:24:46.450 回答