第十三课 python+requests(13.1)
一、介绍request库
(1)requests是用python语言编写的简单易用的http库,用来做接口测试的库;
(2)接口测试自动化库有哪些?
requests、urllib 、urllib2、urllib3、 httplib 等(最受欢迎的是requests)
(3)安装request库
方式一:
dos下pip:
安装命令:pip install requests
查看命令:pip list
方法二:pycharm 中 setting下载
(4)组建一个接口需要参数?
a、请求方式
b、url
c、请求头
d、请求参数
二、requests中的运行
1、导入requests 模块
2、运行的三种方法:
(1)第一种方法:
requests.post(url=url,data=data,json=heardes)
a、讲解post接口
ctrl+点击post方法查看使用方法
案例:
(1)import requests
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
h={“Content-Type”:“application/x-www-form-urlencoded”}
data={‘userAccount’:‘admin’,‘loginPwd’:‘123456’}
jk=requests.post(url=url,data=data,json=h)
print(jk.text) #响应体内容
print(jk.json())#json格式内容
print(jk.cookies) # 打印接口的cookies值
print(jk.status_code) # 接口的响应状态码
print(jk.request) #打印接口的post请求
print(jk.url) #打印接口的url
b、get 接口
ctrl+鼠标点击get查看方法
get接口
import requests
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456”
h={“Content-Type”:“application/x-www-form-urlencoded”}
jk=requests.get(url=url,json=h)
print(jk.text) #响应体内容
print(jk.json())#json格式内容
print(jk.cookies) # 打印接口的cookies值
print(jk.status_code) # 接口的响应状态码
print(jk.request) #打印接口的post请求
print(jk.url) #打印接口的url
get的parms的写法:
import requests
url="http://cms.duoceshi.cn/manage/loginJump.do"
data={'userAccount':'admin','loginPwd':'123456'}
h = {'Content-Type': "application/x-www-form-urlencoded"}
r=requests.get(url=url,params=data,json=h)
print(r.text) #{"code":"200","msg":"登录成功!","model":{}}
print(r.json()) #{'code': '200', 'msg': '登录成功!', 'model': {}}
print(r.cookies) #<RequestsCookieJar[<Cookie JSESSIONID=B5F856D5B4013D37ACF9ADAA04BA3018 for cms.duoceshi.cn/>]>
print(r.status_code) #200
print(r.url) #http://cms.duoceshi.cn/manage/loginJump.do
print(r.request) #<PreparedRequest [POST]>
(2)第二种方法
post请求方法:requests.request(“post”,url=url,data=data,json=h)
get请求方法:requests.request(“get”,url=url,params=h)
post请求方法
requests.request(“post”,url=url,data=data,json=h)
案例:
import requests
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
h={“Content-Type”:“application/x-www-form-urlencoded”}
data={‘userAccount’:‘admin’,‘loginPwd’:‘123456’}
jk=requests.request(“post”,url=url,data=data,json=h)
print(jk.text) #响应体内容
print(jk.json())#json格式内容
print(jk.cookies) # 打印接口的cookies值
print(jk.status_code) # 接口的响应状态码
print(jk.request) #打印接口的post请求
print(jk.url) #打印接口的url
get 请求方法
import requests
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456”
h={“Content-Type”:“application/x-www-form-urlencoded”}
jk=requests.request(“get”,url=url,params=h)
print(jk.text) #响应体内容
print(jk.json())#json格式内容
print(jk.cookies) # 打印接口的cookies值
print(jk.status_code) # 接口的响应状态码
print(jk.request) #打印接口的post请求
print(jk.url) #打印接口的url
(3)第三种方法
格式:requests.Session()
#登录接口
import requests
s=requests.Session() #使用session保持上下文管理,可以保持会话的状态
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456”
h={“Content-Type”:“application/x-www-form-urlencoded”}
jk=s.get(url=url,params=h)
print(jk.text) #响应体内容
#查询栏目接口
url2=“http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
h={“Content-Type”:“application/x-www-form-urlencoded”}
data2={“parentId”:“”,‘categoryName’:“”,“page”:“1”}
jk2=s.post(url=url2,data=data2,json=h)
print(jk2.text)
#封装在一个类中:
import requests
s=requests.Session()
class cms(object):
def init(self):
pass
def dl(self):# 登录接口
url = “http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456”
h={“Content-Type”:“application/x-www-form-urlencoded”}
jk=s.get(url=url,params=h)
print(jk.text)
def lmcx(self):
url2 = “http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
h={“Content-Type”:“application/x-www-form-urlencoded”}
data2={“parentId”:“”,‘categoryName’:“”,“page”:“1”}
jk2=s.post(url=url2,data=data2,json=h)
print(jk2.text)
if name == ‘main’:
dx=cms()
dx.dl()
dx.lmcx()
python+reques断言
(1)assert 断言
(2)if语句断言
备注:需要将内容转换成json格式或text文档格式,在进行断言
if断言的案例:
import requests
s=requests.Session()
class Api(object):
def __init__(self):
pass
def dl(self):
url="http://cms.duoceshi.cn/manage/loginJump.do"
data={'userAccount':'admin','loginPwd':'123456'}
h = {'Content-Type': "application/x-www-form-urlencoded"}
r=s.request("get",url=url,params=data,json=h)
js=r.json()#先转换成json
# print(js)
if js["msg"]=="登录成功!":
print("ok")
else:
print("no")
def cxyh(self):
url2="http://cms.duoceshi.cn/manage/queryUserList.do"
data2={'startCreateDate':"","endCreateDate":"","searchValue":
"","page":"1"}
h2 = {'Content-Type': "application/x-www-form-urlencoded"}
r2=s.request("post",url=url2,data=data2,json=h2)
print(r2.text)
if __name__ == '__main__':
dx=Api()
dx.dl()
# dx.cxyh()
assert断言
import requests
s=requests.Session()
class Api(object):
def __init__(self):
pass
def dl(self):
url="http://cms.duoceshi.cn/manage/loginJump.do"
data={'userAccount':'admin','loginPwd':'123456'}
h = {'Content-Type': "application/x-www-form-urlencoded"}
r=s.request("get",url=url,params=data,json=h)
js=r.json()#先转换成json
# print(js)
assert js["msg"]=="登录成功!"
print(js)
def cxyh(self):
url2="http://cms.duoceshi.cn/manage/queryUserList.do"
data2={'startCreateDate':"","endCreateDate":"","searchValue":
"","page":"1"}
h2 = {'Content-Type': "application/x-www-form-urlencoded"}
r2=s.request("post",url=url2,data=data2,json=h2)
print(r2.text)
if __name__ == '__main__':
dx=Api()
dx.dl()
# dx.cxyh()
依赖接口:
(1)通过requests.Session() 方法
(2)提取cookie ,在下一个接口的请求头调用cookie调用 (每一个接口都要被调用编写一次)
.#提取cookie的方法做依赖接口
import requests
class Api(object):
def __init__(self):
pass
def dl(self):
url = "http://192.168.163.128:8080/cms/manage/loginJump.do"#登陆
data = {'userAccount': 'admin', 'loginPwd': '123456'}
h = {'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8"}
r = requests.request("get", url=url, params=data, json=h)
c = str(r.cookies) # 提前cookie值
self.cook = c.split(" ")[1] # 对cookie值进行分割,取cook的值
print(self.cook) # JSESSIONID=D18ED7574E8EB9BB0C6F38EF5F6ECA80
def cxyh(self):
url2 = "http://192.168.163.128:8080/cms/manage/queryUserList.do"
data2 = {'startCreateDate': "", "endCreateDate": "", "searchValue":"", "page": "1"}
# 下一个接口的请求头加上cookie值,保持会话
h2 = {'Content-Type': "application/x-www-form-urlencoded", 'Cookie': self.cook}
r2 = requests.request("post", url=url2, data=data2, headers=h2)
# print(r2.headers)
print(r2.text)
if __name__ == '__main__':
dx = Api()
dx.dl()
dx.cxyh()
课:
import requests
class jk1(object):
def __init__(self):
pass
def dl(self):
url=r"http://192.168.163.128:8080/cms/manage/loginJump.do"
data={'userAccount':'admin','loginPwd':'123456'}
headers={"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}
dx=requests.request(method="post",url=url,data=data,headers=headers)
print(dx.text) #{"code":"200","msg":"登录成功!","model":{}}
c=str(dx.cookies)
self.s=c.split(" ")[1]
# return s
def yh(self):
url2="http://192.168.163.128:8080/cms/manage/queryUserList.do"
data2={"startCreateDate":"","endCreateDate":"","searchValue":"","page":1}
headers2={"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8","Cookie":self.s}
dx2=requests.request(method="post",url=url2,data=data2,headers=headers2)
print(dx2.text)#{"code": "200", "msg": "查询用户成功!", "model": {"totalRow": 2,…}}
if __name__ == '__main__':
dx=jk1()
dx.dl()
dx.yh()
==================================================
关联接口
(1)身份接口和城市接口:
写死的场景:
class gl(object):
def __init__(self):
pass
def sf(self):
url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince"
h = {'Content-Type': "application/x-www-form-urlencoded"}
r=requests.get(url=url,headers=h)
print(r.text)
def cs(self):
url2="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity"
data2={"byProvinceName":"浙江"}
h2 = {'Content-Type': "application/x-www-form-urlencoded"}
r=requests.post(url=url2,data=data2,headers=h2)
print(r.text)
if __name__ == '__main__':
dx=gl()
dx.sf()
dx.cs()
另一种:
import requests
s=requests.session()
class dm(object):
def __init__(self):
pass
def sf(self):
url1=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince"
headers1={"Content-Type":"application/x-www-form-urlencoded"}
j=s.request(method="get",url=url1,headers=headers1)
print(j.text)
def cs(self):
url2=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity"
data2= {'byProvinceName':'浙江'}
headers2 = {"Content-Type": "application/x-www-form-urlencoded"}
j1=s.request(method="post",url=url2,data=data2,headers=headers2)
print(j1.text)
if __name__ == '__main__':
dx=dm()
dx.sf()
dx.cs()
re正则提取,导入re模块
import requests
import re
class gl(object):
def __init__(self):
pass
def sf(self):
url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince"
h = {'Content-Type': "application/x-www-form-urlencoded"}
r=requests.get(url=url,headers=h)
print(r.text)
tq=re.findall('<string>(.+?)</string>',r.text)
print(tq)
return tq
def cs(self):
s=self.sf()
url2="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity"
data2={"byProvinceName":s[2]}
h2 = {'Content-Type': "application/x-www-form-urlencoded"}
r=requests.post(url=url2,data=data2,headers=h2)
print(r.text)
if __name__ == '__main__':
dx=gl()
dx.sf()
dx.cs()
另一种1:
import requests
import re
s=requests.session()
class dm(object):
def __init__(self):
pass
def sf(self):
url1=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince"
headers1={"Content-Type":"application/x-www-form-urlencoded"}
j=s.request(method="get",url=url1,headers=headers1)
wb=re.findall("<string>(.+?)</string>",j.text)
return wb
def cs(self):
url2=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity"
data2= {'byProvinceName':self.sf()[5]}
headers2 = {"Content-Type": "application/x-www-form-urlencoded"}
j1=s.request(method="post",url=url2,data=data2,headers=headers2)
print(j1.text)
if __name__ == '__main__':
dx=dm()
dx.sf()
dx.cs()
另一种2:
import requests
import re
s=requests.session()
class dm(object):
def __init__(self):
pass
def sf(self):
url1=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince"
headers1={"Content-Type":"application/x-www-form-urlencoded"}
j=s.request(method="get",url=url1,headers=headers1)
self.wb=re.findall("<string>(.+?)</string>",j.text)
def cs(self):
url2=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity"
data2= {'byProvinceName':self.wb[4]}
headers2 = {"Content-Type": "application/x-www-form-urlencoded"}
j1=s.request(method="post",url=url2,data=data2,headers=headers2)
print(j1.text)
if __name__ == '__main__':
dx=dm()
dx.sf()
dx.cs()