requests实现cookies鉴权-学习笔记总结

    技术2022-07-13  78

    import requests # 第一种方法 # 第一步 实例化Session()对象 sess = requests.Session() # 得到cookies鉴权(即cookies值) logon_url = "https://www.ketangpai.com/UserApi/login" logon_data = {"email":"XXXXXX","password":"XXXXXX","remember":"1"} # email,password 根据实际数据填写 req = sess.post(url=logon_url,data=logon_data) print("登录响应的cookies值:",req.cookies) print("登录之后的cookies值:",sess.cookies) # 会主动讲响应后的set_cookies值添加到sess对象中 # 第二步 获取用户信息 user_url = "https://www.ketangpai.com/UserApi/getUserInfo" request = sess.get(url=user_url) print(request.json()) # 第二种方法 相比第一种方法,自己主动取cookies值,并在后续的请求中,主动添加上cookies值 # 第一步 登录得到cookies鉴权 logon_url = "https://www.ketangpai.com/UserApi/login" logon_data = {"email":"XXXXXX","password":"XXXXXX","remember":"1"} req = requests.post(url=logon_url,data=logon_data) # 主动取cookies值 cookies = req.cookies # 第二步 获取用户信息 user_url = "https://www.ketangpai.com/UserApi/getUserInfo" req = requests.get(url=user_url,cookies=cookies) print(req.json())
    Processed: 0.009, SQL: 9