素早い回答
オプション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)
、それを を介して Cookie jar に追加し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)]>