0

我正在尝试在网站上搜索“Adrian”一词。我已经明白,首先我必须向网站发送一个请求,在响应中我将有一个 XSRF 令牌,我需要将它用于第二个请求。据我了解,如果我使用 session.get() 它也会自动为第二个请求保留 cookie。我运行第一个请求,得到 200 响应,我打印出 cookie,令牌就在那里。我运行第二个请求,我得到一个 400 错误,但是如果我打印出第二个请求的标头,则令牌就在那里。我不知道哪里出了问题。为什么我第二个得到400?

import requests  
session = requests.Session()

response = session.get('https://www.racebets.com/en/horse-racing/formguide') 
print(response)  
cookies = session.cookies.get_dict()  
print(cookies) 
XSRFtoken = cookies['XSRF-TOKEN']  
print(XSRFtoken)

response = session.get('https://www.racebets.com/ajax/formguide/search?s=Adrian') 
print(response)  
print(response.request.headers)

我还尝试跳过会话并在第二个请求中使用 requests.get() 并自己将令牌添加到标头,但结果是相同的:

import requests 
session = requests.Session()
 
response = session.get('https://www.racebets.com/en/horse-racing/formguide')
print(response) 
cookies = session.cookies.get_dict() 
print(cookies)
XSRFtoken = cookies['XSRF-TOKEN'] 
print(XSRFtoken)
 
headers = {'XSRF-TOKEN': XSRFtoken} 
response = session.get('https://www.racebets.com/ajax/formguide/search?s=Adrian', headers=headers)
print(response) 
print(response.request.headers)
4

0 回答 0