3.25-3 request断言
一.request断言
if断言
案例:
import requests s=requests.Session() url1="http://49.233.201.254:8080/cms/manage/loginJump.do" data1={'userAccount':'admin','loginPwd':'123456'} h1={"Content-Type":"application/x-www-form-urlencoded"} jk1=s.request("post",url=url1,data=data1,headers=h1) wb=jk1.json() print(jk1.json()) if wb["msg"]=="登录成功!": print("ok") else: print("no")
二.assert断言
h1={"Content-Type":"application/x-www-form-urlencoded"} jk1=s.request("post",url=url1,data=data1,headers=h1) wb=jk1.json() print(jk1.json()) assert wb["msg"]=='登录成功' print(jk1.cookies)
二.封装接口
案例1:requests.Session()
import requests s=requests.Session() class Cms(object): def __init__(self): pass def dl(self): url1 = "http://49.233.201.254:8080/cms/manage/loginJump.do" data1={'userAccount':'admin','loginPwd':'123456'} h1={"Content-Type":"application/x-www-form-urlencoded"} jk1=s.request("post",url=url1,data=data1,headers=h1) print(jk1.json()) def yh(self): url2="http://49.233.201.254:8080/cms/manage/queryUserList.do" data2={ 'startCreateDat':'', 'endCreateDate':'', 'searchValue':'', 'page':'1' } h2={"Content-Type":"application/x-www-form-urlencoded"} jk2=s.request("post",url=url2,data=data2,headers=h2) print(jk2.text) if __name__ == '__main__': dx=Cms() dx.dl() dx.yh()
案例2:
cookies 保持会话
案例:
import requests class Cms(object): def __init__(self): pass def dl(self): url1 = "http://49.233.201.254:8080/cms/manage/loginJump.do" data1={'userAccount':'admin','loginPwd':'123456'} h1={"Content-Type":"application/x-www-form-urlencoded"} jk1=requests.request("post",url=url1,data=data1,headers=h1) print(jk1.text) c=str(jk1.cookies) print(c) #<RequestsCookieJar[<Cookie JSESSIONID=DAB3F94092C907F3BAD009410A8778E0 for 49.233.201.254/cms/>]> self.cookie=c.split(" ")[1] #JSESSIONID=6D60B90E937058D8B91B66A18671A4CB # print(cookie) def yh(self): url2="http://49.233.201.254:8080/cms/manage/queryUserList.do" data2={ 'startCreateDat':'', 'endCreateDate':'', 'searchValue':'', 'page':'1' } h2={"Content-Type":"application/x-www-form-urlencoded", "Cookie":self.cookie} jk2=requests.request("post",url=url2,data=data2,headers=h2) print(jk2.text) if __name__ == '__main__': dx=Cms() dx.dl() dx.yh()
案例3:
import requests class Cms(object): def __init__(self): pass def dl(self): url1 = "http://49.233.201.254:8080/cms/manage/loginJump.do" data1={'userAccount':'admin','loginPwd':'123456'} h1={"Content-Type":"application/x-www-form-urlencoded"} jk1=requests.request("post",url=url1,data=data1,headers=h1) print(jk1.text) c=str(jk1.cookies) print(c) #<RequestsCookieJar[<Cookie JSESSIONID=DAB3F94092C907F3BAD009410A8778E0 for 49.233.201.254/cms/>]> cookie=c.split(" ")[1] #JSESSIONID=6D60B90E937058D8B91B66A18671A4CB # print(cookie) return cookie def yh(self): d=self.dl() url2="http://49.233.201.254:8080/cms/manage/queryUserList.do" data2={ 'startCreateDat':'', 'endCreateDate':'', 'searchValue':'', 'page':'1' } h2={"Content-Type":"application/x-www-form-urlencoded", "Cookie":d} jk2=requests.request("post",url=url2,data=data2,headers=h2) print(jk2.text) if __name__ == '__main__': dx=Cms() # dx.dl() dx.yh()
关联接口
省份接口:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince城市接口http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupport 入参CitybyProvinceName:{{cs}}
import requests s=requests.Session() class Gl(object): def __init__(self): pass def sf(self): url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince" jk=s.post(url=url) print(jk.text) def cs(self): url1=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity" data1={'byProvinceName':"浙江"} h1={"Content-Type":"application/x-www-form-urlencoded"} jk1=s.post(url=url1,data=data1,) print(jk1.text) if __name__ == '__main__': dx=Gl() dx.sf() dx.cs()
案例2:
import requests import re s=requests.Session() class Gl(object): def __init__(self): pass def sf(self): url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince" jk=s.post(url=url) print(jk.text) self.tq=re.findall('<string>(.+?)</string>',jk.text) # print(tq) def cs(self): url1=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity" data1={'byProvinceName':self.tq[8]} h1={"Content-Type":"application/x-www-form-urlencoded"} jk1=s.post(url=url1,data=data1,headers=h1) print(jk1.text) if __name__ == '__main__': dx=Gl() dx.sf() dx.cs()