接口自动化测试(二)
一. 为请求添加cookies
在上一篇文章中,可以看出,如果不给request请求加上cookie,当我们获取登录后的页面的考勤信息时,会自动跳转到登录页面。由于http是无状态协议,cookie的作用就是跟踪会话
注意cookies也要按照**kwargs传参的方式,即以key=value的形式传入,其中value是字典形式,这和params比较类似
import requests #调用登录接口——生成cookies #从响应的消息头中,获取身份令牌 url = "https://www.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGy9yHz7ev" params = {"courseid": "MDAwMDAwMDAwMLOGy9yHz7ev"} cookies = {"ketangpai_home_remember": "think%3A%7B%22username%16293798223OGpZaIudFqhc6Gl7LQetmZtmfOk2RhbQ%292038012%22expire%22%3A%22MDAwMDAwMDAwMLOGud6Gub9phd523rG2dZ4%22%2C%22token%22%3A%22MDAwMDAwMDAwMMurrpWavLehhs1-3LLfgduEt4OWepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p-q6iZu2yrn4uNhJ3KedDYk7ivboS4it6xuZGShN1z3n6KYW0%22%7D"} res = requests.request("get", url, params=params, cookies=cookies) print(res.status_code) print(res.text.encode('latin-1').decode('unicode_escape'))
运行结果
200 {"status":1,"data":[{"id":"MDA193023MLR2qZ18983","title":"2018.07.13","createtime":"1531484669","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2qZaHqauw","title":"2018.07.11","createtime":"1531312035","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2qZWHz81s","title":"2018.07.09","createtime":"1531139222","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2qZSIuc1s","title":"2018.07.06","createtime":"1530879957","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2qd6IubNo","title":"2018.07.04","createtime":"1530707056","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2qd2IubOv","title":"2018.07.02","createtime":"1530534412","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2qd2Gqadt","title":"2018.06.29","createtime":"1530275097","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2qduHudFq","title":"2018.06.27","createtime":"1530102228","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2pZmGqa-w","title":"2018.06.25","createtime":"1529929607","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2pZeIqdFo","title":"2018.06.22","createtime":"1529672088","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2pZWHz82x","title":"2018.06.20","createtime":"1529499309","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2pd6Gqbey","title":"2018.06.15","createtime":"1529065618","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLR2pduG36-w","title":"2018.06.13","createtime":"1528892832","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsz5eGz9Gy","title":"2018.06.11-jenkins","createtime":"1528719713","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsz5WG39Fo","title":"2018.06.08-接口自动化(五)","createtime":"1528460682","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsz92Gz9Fp","title":"2018.06.06-接口实战(四)","createtime":"1528287924","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsy5mGqa-x","title":"2018.06.04-接口自动化实战(三)","createtime":"1528115207","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsy96H379o","title":"2018.05.30--接口实战(二)","createtime":"1527683074","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsy9uG37uw","title":"2018.05.28--接口自动化实战","createtime":"1527510322","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsx5iGz7ux","title":"2018.05.25--接口测试","createtime":"1527251091","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsx5SIqc2y","title":"2018.05.23-接口测试","createtime":"1527078219","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsx9yGubtt","title":"2018.05.21-作业+邮件","createtime":"1526905679","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsvZiIqb-x","title":"2018.05.18--复习课","createtime":"1526646290","type":"1","state":"3"},{"id":"MDAwMDAwMDAwMLOsvZWG369r","title":"2018.05.16-配置+logger","createtime":"1526473605","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsvdyHudGx","title":"2018.05.14-数据库+配置","createtime":"1526300819","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsuZmGqdGw","title":"2018.05.11-单元测试+excel操作","createtime":"1526041334","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsuZWHqaey","title":"2018.05.09-单元测试","createtime":"1525868625","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsudyHz6ex","title":"2018.05.07-类和对象-单元测试","createtime":"1525695789","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOstZWHuclq","title":"2018.05.02-类和对象(二)","createtime":"1525263806","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOstdyHz7ex","title":"2018.04.27-类和对象 一","createtime":"1524831912","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOssZeIqdGv","title":"2018.04.25-os操作异常处理","createtime":"1524658975","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOssd6Iqbto","title":"2018.04.23-作业讲解+文件操作","createtime":"1524486144","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOssdyG369s","title":"2018.04.20-嵌套循环和函数","createtime":"1524227178","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsrZiGuc2x","title":"2018.04.18 - 控制流","createtime":"1524054279","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsrZSHqatq","title":"2018.04.16-作业讲解+数据类型","createtime":"1523881366","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsrdyHz81s","title":"2018.04.13 - 列表和字典","createtime":"1523622079","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsqZiGuadr","title":"2018.04.11-python环境和基本语法","createtime":"1523449326","type":"1","state":"0"},{"id":"MDAwMDAwMDAwMLOsqd6Iuc1q","title":"2018.04.09-开学典礼","createtime":"1523274686","type":"1","state":"0"}],"attenceCount":37,"lateCount":0,"absentCount":0,"pleaseCount":1,"privateLeaveCount":0,"sickLeaveCount":0,"statutoryCount":0,"leaveEarlyCount":0,"info":"success"}
二. 从响应结果中获取cookies
此外,还可以通过响应结果来获取cookies
import requests url = "https://fanyi.baidu.com" res = requests.request("get", url) #res.cookies获得的是RequestCookieJar的实例 print(res.cookies) print(type(res.cookies)) #从响应结果当中获取cookies中键值对的值——参数为键名 print(res.cookies.get("BAIDUID"))
运行结果
<RequestsCookieJar[<Cookie BAIDUID=BEE625F269183991307D1097F72E:FG=1 for .baidu.com/>, <Cookie locale=zh for .baidu.com/>]> <class 'requests.cookies.RequestsCookieJar'>
BEE625F269183991307D1097F72E:FG=1