requests表单提交
1, 头部中的content-type设置为表单
"Content-Type": "application/x-www-form-urlencoded",
2,要提交的表单数据写成json格式
调用requests.post接口,将data赋值为我们的表单数据
url = "http://xxxx/signin"
data = {
"username": "xxx",
"password": "xxx1111",
}
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
}
try:
resp = requests.post(url, data=data, headers=headers)
except requests.exceptions.RequestException:
print("request exception")
except:
print("request other exception")
2,有些登入接口,是返回302的,requests在遇到302状态时候会跳转,不需要跳转的话,就设置
allow_redirects=False
resp = requests.post(url, data=data, headers=headers, allow_redirects=False)