既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
模拟post请求编辑标签
import requests,j son,jsonpath
url_ aprams={“access_ token”:"57135BrbDZbsPz0iM9LP4icmRRMuRubgXA- rBbKXkvE5HT4fqRT}
post_ data = {“tag” : {“id”:103,“name” :“北京老帽”}}
#先将json数据转换成字符串
post_ str = json.dumps(post_data,ensure_ ascii=False)
response = requests.post(url =“https:/ /api.weixin.qq.com/cgi-bin/tags/update”,Darams= urL_ aprams,
json=post_ data #json必须传 json数据
#利用data传入字符串数据 用encode('utf-8.)解决乱码格式
data=post_ str .encode(‘utf-8’))
print (response. text)
### 六、获取响应正文text/content/json()/raw
获取响应信息
响应行 响应头 响应正文
import requests
response=requests.get(url=“http://www.baidu.com”)
print(response.url) #获取url
print(response.headers) #.get() []去获取单独的头
print(response.encoding) #获取网页编码格式1
requests根据 响应信息头中的 charset 字段判断网页编码格式 如果没有该字段 则默认为ISO-8859-1 具体表现为乱码
print(response.apparent_encoding) #获取网页编码格式2
requests根据 响应正文中的 charset 进行获取网页编码格式
response.encoding=response.apparent_encoding
把响应正文的编码类型设置为由响应正文的charset字段获取
print(response.text) #以字符串文本的方式获取响应正文
以二进制的方式获取响应正文 (普通网页、下载文件、图片、视频等二进制文件 )
print(response.content.decode(‘utf-8’))
响应正文为二进制数据
import requests
from PIL import Image #pil操作图片
from io import BytesIO #字节流
response=requests.get(url=“https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170719%2F2f4b26eebc3c4c63b25b7e98d65a0cb1_th.jpg&refer=http%3A%2F%2Fimg.mp.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655610368&t=75ee6522d3101672a7ae0667b676c6e6”)
img_bytes=BytesIO(response.content) #把获取的二进制图片对象转换成字节流
img_ogj=Image.open(img_bytes) #创建图像对象 打开字节流图片
img_ogj.save(‘test.png’) #保存图片
响应正文为json数据
import requests,jsonpath
url_params={
“grant_type”:“client_credential”,
“appid”:“wxb61e6693c7244793”,
“secret”:“c146872139641af2be50a94a01aee9a7”
}
response=requests.get(url=“https://api.weixin.qq.com/cgi-bin/token”
,params=url_params)
如果已知道返回的数据格式是json格式的正文 可以直接返回json对象
json_body=response.json()
token_value=jsonpath.jsonpath(json_body,“$.access_token”)[0]
print(token_value)
响应正文返回原始数据 raw 几乎不用
import requests
response=requests.get(url=“https://www.baidu.com”,stream=True)
print(response.raw.read(10)) #返回原始套接字内容
### 七、pil 操作图片
import requests
from PIL import Image #pil操作图片
from io import BytesIO #字节流
response=requests.get(url=“https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170719%2F2f4b26eebc3c4c63b25b7e98d65a0cb1_th.jpg&refer=http%3A%2F%2Fimg.mp.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655610368&t=75ee6522d3101672a7ae0667b676c6e6”)
img_bytes=BytesIO(response.content) #把获取的二进制图片对象转换成字节流
img_ogj=Image.open(img_bytes) #创建图像对象 打开字节流图片
img_ogj.save(‘test.png’) #保存图片
### 八、代理设置
requests 设置代理 必须有代理服务器打开
import requests
proxy_server={
“http”:“http://127.0.0.1:8888”,
“https”:“https://127.0.0.1:8888”
}
response=requests.get(url=“http://192.168.0.17/phpwind”,
proxies=proxy_server)
带密码的代理 http://用户名:密码@127.0.0.1:8888
### 九、超时处理
超时设置
import requests
reponse = requests.get(url=“http://192.168.0.17/phpwind”,
# timeout=0.001)#timeout 设置的是float 响应时间是响应超时设置
timeout=(0.05, 0.04)) # timeout 设置的是tuple
0.05是链接服务器超时时间 0.04表示响应超时时间设置
print(reponse.text)
### 十、重定向
requests 是默认支持重定向操作
import requests
allow_redirects 默认设置为true 支持重定向 true打开 false关闭
response=requests.get(url=“http://www.360buy.com”,allow_redirects=False)
print(response.history) #查看重定向过程
print(response.content.decode(‘utf-8’))
requests在模拟https请求时
如果要模拟的系统需要进行证书认证(极少网站)才能访问
那么此时汇报如下错误 SSL error
![超详细python接口自动化测试requests实战教学(详全)](https://img-blog.csdnimg.cn/dea3ee5ef12c4411b118b117e53dc235.png)
![超详细python接口自动化测试requests实战教学(详全)](https://img-blog.csdnimg.cn/6aa9f3644e6046ea97015c7911a27e23.png)
处理方式: 处理方式一:使用verifyFalse 忽略证书认证过程
![超详细python接口自动化测试requests实战教学(详全)
](https://img-blog.csdnimg.cn/8a5beb21c5e0451c8b0ff75978018201.png)
新版本的urllib3 不支持 要使用方式一 需要降级urllib3
pip install urllib3==1.25.7
处理方式二:使用cert=证书路径 来解决
![超详细python接口自动化测试requests实战教学(详全)](https://img-blog.csdnimg.cn/2d0a78f8cad94d258f714cb9528cff33.png)
### 十一、自带的异常处理
requests自带的异常处理
Exception 是所有异常类的父类
RequestException 是requests 包 异常类的父类
import requests
from requests.exceptions import InvalidURL
from requests.exceptions import RequestException
try:
response = requests.get(url=“https: //www.baidu.com”)
except InvalidURL as e: #子–子
print(‘无效的url连接’)
except RequestException as e: #子
print(‘请求异常,具体原因未知’)
except Exception as e: #父
print(‘系统异常,具体原因未知’)
print(response.text)
最后: 可以在公众号:伤心的辣条 ! 自行领取一份216页软件测试工程师面试宝典文档资料【免费的】。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。
![img](https://img-blog.csdnimg.cn/img_convert/d1a0099e2efcfdabc815c88b348981e0.png)
![img](https://img-blog.csdnimg.cn/img_convert/9c07ece4627ad110a72112c680753a27.png)
![img](https://img-blog.csdnimg.cn/img_convert/3bca24885562ce75e7590ad071458a06.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**