requests是http的一个客户端软件(python中的一个工具),可以用它来做爬虫、还可以进行接口的自动化测试。
python+requests+unittest+ddt+json等实现接口的自动化测试。
2、requests安装cmd-->pip3 install requests
3、验证requests的安装 二、requests的基本用法百度首页接口请求的实现。
1、使用命令行来实现(cmd)通过下面的命令行,就可以获取响应报文中的基本所有数据了。
python
import requests
//res: 响应报文对象(响应报文的内容都保存在这个变量里了)
//requests.get() : 打包并发送请求的过程(postman,输入请求方式、url后点击send)
res = requests.get("https://www.baidu.com")
查看返回的响应状态码:print(res.status_code)
查看返回响应正文:print(res.text)
查看返回响应头部字段:print(res.headers)
查看返回响应头部Content-Type字段:print(res.headers["Content-Type"])
查看响应正文的编码信息:print(res.encoding)
2、安装IDE工具(vscode、pycharm等)# 还是百度的首页接口
# 导包
import requests
# 打包并发送请求(get),返回响应结果保存在一个变量中(可以叫做响应报文对象)
# response:就是一个变量
response = requests.get("https://www.baidu.com")
# 先查看对象()
# print(response)
# 查看该响应结果对象中的状态码是多少
# print(response.status_code)
# 查看响应正文(以字符串的形式返回)
# print(response.text)
# ISO-8859-1:该编码格式的中文字符不能在控制台正常显示
# print(response.encoding
# 需要将字符集转为utf-8类型
# response.content是转为bytes字节码形式
# print(response.content.decode("utf-8"))
# 获取响应头部的所有的字段(text/html)
# print(response.headers["Content-Type"])
# 获取cookies和url
# ]>
print(response.cookies)
# https://www.baidu.com/
print(response.url)
3、requests提供的常见请求方式
requests.request("GET",url,*args)
requests.get(url,*args) :处理http协议中的get请求
requests.post(url,*args): 处理http协议中post请求
requests.put(url,*args)
requests.delete(url,*args)
requests.head(url,*args)
三、使用requests实现http接口请求# 百度翻译接口--get请求 # 打包这个请求,只需要提供url地址和请求方式(get) from urllib import response import requests url = "https://fanyi-api.baidu.com/api/trans/vip/translate?q=apple2&from=auto&to=jp&appid=20190630000313415&salt=888888&sign=a9adc2d687fbacecc3b5059b9ccedc95" method = "GET" # 使用第一种范式发送该get请求 # requests.request():以位置参数的形式传递的数据 # request()的参数有十多个,如果你按照顺序传递,可以使用位置传参;如果不像按照顺序传只能使用关键字传参 # response = requests.request(method,url) # 使用第二种范式发送该get请求 response = requests.get(url) ## 符合json结构特点:只能由[]、{}、""组成的数据结构 # 如果你的响应正文是符合json结构特点的,可以使用text和json() # 如果你的响应正文不符合json数据结构特点,只能使用text # response.text : 将响应正文转为字符串格式 # {"error_code":"54001","error_msg":"Invalid Sign"} # strData = response.text # print(type(strData)) # print(strData) # response.json():将符合json数据结构的数据转为python可处理的字典或者列表类型 jsonData = response.json() # print(type(jsonData)) # 54001 print(jsonData["error_code"]) # 断言 actualValue = jsonData["error_code"] expectValue= "54001" if actualValue==expectValue: print("用例通过") else: print("用例失败")
3、视频评论接口(referer)
# 千锋视频评论接口-get
# 打包请求的要求:url地址、请求方式(get)、headers(referer)
import requests
# 定义url参数
url = "https://ke.qq.com/cgi-bin/comment_new/course_comment_list?cid=302090&count=5&page=1&filter_rating=1&bkn=&r=0.9658793132640322"
# 以字典的形式来定义请求头部中的字段
headers = {
"referer":"https://ke.qq.com/course/302090?tuin=99579027&taid=2237639306812426"
}
# 直接打包该get请求,并发送
# 只能使用关键字传参的方式给值
# headers=headers:等号的前面是关键字(方法声明时候定义的形式参数名),等号的后面是该脚本中定义的变量
response = requests.get(url,headers=headers)
# 查看响应正文,以字符串形式显示
# {"msg":"refer错误","type":1,"retcode":100101}
# strData是大串,expectValue是小串
# strData = response.text
# expectValue = "超好 对小白来说 容易懂 感谢老王,老王辛苦了"
# # python中如何实现判断大串包含小串的用法?用成员运算符in
# if expectValue in strdata:
# print("测试用例通过")
# else:
# print("测试用例失败")
# 大家试一下,将正文转为字典和列表形式,如何判断
jsonData = response.json()
# 第一条评论的实际值
actualValue = jsonData["result"]["items"][0]["first_comment"]
expectValue = "超好 对小白来说 容易懂 感谢老王,老王辛苦了"
if actualValue == expectValue:
print("用例通过")
else:
print("用例失败")
4、电商注册接口(post)
# 电商注册接口-post
# 打包该接口需要哪些参数:
# 请求地址(包括query string parameters)
# 请求方式
# 请求头部
# 请求的数据
import requests
# 请求地址(包括query string parameters)
url = "http://39.101.167.251/qftest/index.php?c=user&a=register&step=submit"
# 请求方式--post
# 请求头部
headers = {
"Content-Type":"application/x-www-form-urlencoded"
}
# 请求的数据(相当于postman种的body),也是以字典的形式传参
# 该条是用户名为空的接口用例
data = {
"username":"",
"email":"bk2105_000@163.com",
"password":"123456",
"repassword":"123456",
"agree":"on"
}
# 把上文参数打包在一起,并发送出去
# 可以全部使用关键字传参
response = requests.post(url=url,data=data,headers=headers)
# 查看响应正文,以字符串
strData = response.content.decode("utf-8")
# print(strData)
# 实际该接口返回的信息是:用户名不符合格式要求
expectValue= "请设置用户名"
if expectValue in strdata:
print("通过")
else:
print("失败")



