我见过如何使用Python中的Requests库发送cookie的帖子,但是在发送多个时却找不到任何内容。我正在尝试捕获整个RequestsCookieJar并将其用作未来帖子的cookie。以下是我目前正在使用的内容:
>>> 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/>]>
我需要在以后使用我的脚本时传递auth_token cookie和_hauavc * cookie。我试过了:
c = {'auth_token': '5227121307', '_hauavc_4699a329': 'b27e38d'}
并使用cookies=c
选项在帖子中发送此内容。我也尝试过设置:
c = s.cookies
并以同样的方式发送。两次尝试都不会引发任何异常,但是当我稍后运行脚本时,它似乎不会确认cookie。我目前正在使用Selenium进程,但还没有能够使用Requests。如果它有帮助,这里是我用Selenium捕获的cookie并使用带有driver.add_cookie()
函数的循环:
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}]
编辑: 包括所要求的完整代码:
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:这是我试图捕获和重用cookie的控制台历史记录:
>>> 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'>[]>