我试图登录到一个网站与python请求,这是成功完成的,但当我试图使Get
或Post
请求在我的帐户内,检索或发布一些数据,我得到服务器响应,我需要登录,这是我刚刚在上一个帖子请求成功,我正在使用session
对象,理论上应该通过服务器在下一个请求中自动发送cookie,也使用适当的标头。
session = requests.Session()
response = session.get( URL , headers=headers)
# I scrape different logging parameters here and I input my own credentials.
login_data = {"username": 'username', "password": 'password', "csrf_token":token}
response = session.post( URL , login_data, headers=headers)
# Login is successful.
response = session.get(URL_2, headers=headers)
Data = {}
response = session.post(URL_2, Data, headers=headers)
# I get server response that I'm not logged in.
该脚本的期望行为是维护一致的日志会话,并能够登录后进行请求
您可以将数据添加到会话中(这里是jwt的示例,但它也适用于cookie):
if response.status_code == HTTPStatus.OK.value:
token_info = response.json()["token"]
session.headers.update(
{"Authorization": f"Bearer {api.token_info['access_token']}"}
)