说明:在登录时不需要cookie 或token,在登录后做其他操作时需要用到token,所以先获取token , 而token 是在登录后的 response 里面,接下来就是要获取response里面的token, 操作如下:
#_*_coding:utf-8_*_
import requests,json
def set_cookie():
header = { # 登录抓包获取的头部
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/json"
}
body = {"account": "XXXXX",
"password": "XXXXXXX"} # 这里账号密码就是抓包的数据
s = requests.session()
login_url = "http://XXXXXXXXXXXXX:8060/api/gv-op/userAuth/login" # 自己找带token网址
login_ret = s.post(login_url, headers=header, data=json.dumps(body))
login_ret.encoding = "utf-8"
rusult = login_ret.json()
accessToken = rusult["data"]["accessToken"]
refreshToken = rusult["data"]["refreshToken"]
# print("accessToken="+accessToken)
# print("refreshToken="+refreshToken)
return accessToken,refreshToken
set_cookie()