我正在尝试使用python请求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)
答案 0 :(得分:14)
我通过导入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)
答案 1 :(得分:6)
要使用内置的函数和方法...
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)
答案 2 :(得分:5)
import requests
s = requests.session()
# Note that domain keyword parameter is the only optional parameter here
cookie_obj = requests.cookies.create_cookie(domain='www.domain.com',name='COOKIE_NAME',value='the cookie works')
s.cookies.set_cookie(cookie_obj)
当询问原始问题时,我不知道此技术是否有效,但理想情况下,您将使用 requests.cookies.create_cookie(name,value,** kwargs)生成自己的Cookie对象然后通过 requests.cookies.RequestsCookieJar.set_cookie(cookie,* args,** kwargs)将其添加到Cookie罐中。请参阅来源/文档here。
>>> 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':'www.domain.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)]>
>>> 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)]>
答案 3 :(得分:2)
plain_cookie = 'nopubuser_abo=1; groupenctype_abo=1'
cj = requests.utils.cookiejar_from_dict(dict(p.split('=') for p in plain_cookie.split('; ')))
sess = requests.Session()
sess.cookies = cj