问题
I've seen posts on how to use the Requests library in Python to send a cookie, but haven't been able to find anything on sending multiple. I'm trying to capture the entire RequestsCookieJar and use it as the cookies for future Posts. Here is what I'm currently working with:
>>> import requests
>>> s = requests.Session()
>>> content = s.get('https://admin.url.com/login.html', verify=False)
>>> print s.cookies
<<class 'requests.cookies.RequestsCookieJar'>[<Cookie _hauavc_4699a329=b27e38d for .url.com/>, <Cookie ut=e for .url.com/>, <Cookie auth_token=5227121307 for admin.url.com/>, <Cookie cwd-extranet=1 for admin.url.com/>, <Cookie slan=en for admin.url.com/>]>
I need to pass both the auth_token cookie and _hauavc* cookie any time I use my script later on. I've tried:
c = {'auth_token': '5227121307', '_hauavc_4699a329': 'b27e38d'}
and sending this in the post with the cookies=c
option. I've also tried setting:
c = s.cookies
and sending it the same way. Both tries do not raise any exceptions, but when I go to run the script later, it doesn't appear to acknowledge the cookies. I currently have the process working with Selenium, but have yet to be able to get it to with with Requests. If it helps, here are the cookies I captured with Selenium and use a loop with the driver.add_cookie()
functions:
c = [{u'domain': u'.url.com', u'name': u'_hauavc_64331bfa', u'value': u'61087df', u'expiry': 1424371385, u'path': u'/', u'secure': True},
{u'domain': u'admin.url.com', u'name': u'auth_token', u'value': u'1138335497', u'expiry': 1424371384, u'path': u'/', u'secure': False}]
EDIT: Including the full code as requested:
import requests
c = {
'_hauavc_64331bfa': '61087df',
'auth_token': '1020504164',
}
s = requests.Session()
content = s.get(
'https://admin.url.com/login.html',
verify=False,
cookies=c
)
soup = Bs(content.text)
session_id = soup.find('input', {'id': 'ses'}).get('value')
payload = {
'loginname': username,
'password': password,
'ses': session_id,
'login': 'Login',
'lang': 'en'
}
content = s.post(
'https://admin.url.com/login.html',
data=payload,
verify=False,
cookies=c
)
EDIT2: And here is the console history of me trying to capture and reuse the cookies:
>>> import requests
>>> s = requests.Session()
>>> c = s.get('https://admin.url.com/login.html', verify=False)
>>> soup = Bs(content.text)
>>> session_id = soup.find('input', {'id': 'ses'}).get('value')
>>> payload = {
... 'loginname': username,
... 'password': password,
... 'ses': session_id,
... 'login': 'Login',
... 'lang': 'en'
... }
>>> content = s.post(
... 'https://admin.url.com/login.html',
... data=payload,
... verify=False
... )
>>> payload = {'phone_id': '2c7f576cde7d1c43234f867d679a961f'}
>>> content = s.post('https://admin.url.com/new_location/verify.html?h_id=777143&ses=%s' % session_id, verify=False, data=payload)>>> payload = {'ask_pin': '674802'}
>>> content = s.post('https://admin.url.com/new_location/confirm.html?h_id=777143&ses=%s' % session_id, verify=False, data=payload)
>>> captured_cookies = s.cookies
>>> s.close()
>>> s = requests.Session()
>>> content = s.get(
... 'https://admin.booking.com/hotel/hoteladmin/login.html',
... verify=False,
... cookies=captured_cookies
... )
>>> soup = Bs(content.text)
>>> session_id = soup.find('input', {'id': 'ses'}).get('value')
>>> payload = {
... 'loginname': username,
... 'password': password,
... 'ses': session_id,
... 'login': 'Login',
... 'lang': 'en'
... }
>>> content = s.post(
... 'https://admin.url.com/login.html',
... data=payload,
... verify=False,
... cookies=captured_cookies
... )
>>> print s.cookies
<<class 'requests.cookies.RequestsCookieJar'>[]>
来源:https://stackoverflow.com/questions/27135753/capture-and-reuse-cookies-with-requests