【发布时间】:2021-04-22 02:35:11
【问题描述】:
在 python 中我有:
cookies = dict(PHPSESSID='PHPSESSID=djsgkjhsdjkhj34',
authchallenge='sdifhshdfiuh34234234',
rishum='skdhfuihuisdhf-' + '10403111')
try:
response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)
但我希望仅在第一次使用 cookie 值,然后使用服务器设置的新值,我该怎么做?
我发现的解决方案不使用默认 cookie 进行首次请求。
注意:我无法自动登录该网站,因为它使用身份验证挑战,所以每次我手动登录并仅针对第一次请求更改这些 cookie,然后当服务器更新它们时我想查看这会影响我当前的 cookie。
我的网站如何运作的示例:
首先我使用 recaptcha 登录,然后获取临时 cookie,
对于我的应用程序中的第一个请求,我想使用这些临时 cookie(已经知道它们)
稍后,每个请求我都需要使用上一个响应中的 cookie(它们随每个请求而变化)
我当前的代码:
def main():
start_time = time.time()
keep_running = True
while keep_running:
keep_running = execute_data()
time.sleep(5.0 - ((time.time() - start_time) % 5.0))
def execute_data():
url = 'https:me.happ.com/rishum/register/confirm'
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'close'
}
cookies = dict(rishum='dsfsdf21312zxcasd-' + '39480523')
try:
response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)
【问题讨论】:
-
注意:我看到了这个:stackoverflow.com/a/31571805 但问题是我只想第一次使用我自己的cookie,然后根据服务器的响应更新它们
-
添加了更多可能有帮助的信息
标签: python python-3.x function session python-requests