***
此次练习不包含使用requests.session()方法实现;
练习环境:本地安装禅道
格式:
1.头部传Cookie:xxx
2.加参数cookies=字典格式
3.直接传RequestsCookieJar,r.cookies
***
直接传RequestsCookieJar,r.cookies
import requests url= "http://127.0.0.1/zentao/user-login.html" s = requests.session() #登录用的参数 body ={ "account":"admin", "password":"e10adc3949ba59abbe56e057f20f883e", "referer":"http://127.0.0.1/zentao/my/" } r_login = s.post(url,data=body) print(r_login.text) print(r_login.content.decode("utf-8")) print(r_login.cookies) # RequestsCookieJar # 获取登陆后的cookies传给下个登陆后的请求 cook = r_login.cookies #可以将cookiesJar转换字典 print(cook) url_my = 'http://127.0.0.1/zentao/my/' r_my = requests.get(url_my,cookies=cook) print(r_my.content.decode("utf-8"))
使用headers方法将cookies传入到下一个请求
import requests #1、头部传cookies,跳过登录,直接进入my页面 url= "http://127.0.0.1/zentao/my" h = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Cookie': 'zentaosid=q771crkvsc8erckfu1vhf4tho0'} #使用头部headers传cookies r_my = requests.get(url,headers=h) print(r_my.content.decode("utf-8"))
使用cookies方法将cookies传入到下一个请求
import requests url= "http://127.0.0.1/zentao/my" h = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Cookie': 'zentaosid=q771crkvsc8erckfu1vhf4tho0'} #2、加参数cookies格式 c ={"zentaosid":"q771crkvsc8erckfu1vhf4tho0"} r_my = requests.get(url,cookies =c) print(r_my.content.decode("utf-8"))