接口测试——requests接口请求(十)

2023-05-16

1. requests库介绍与安装

  1. requests库介绍
  • requests是一款非常火爆且常用的Python三方库
  • 能够实现HTTP协议的各种请求方法
  • 使用简单易上手
  1. requests库的安装方法
  • pip install requests
  • 安装成功
    1675845092467.png

2. requests发送get接口请求

  • 查看百度的响应码:
import requests

response = requests.get("http://www.baidu.com")
print(response)
print(response.status_code)
print(response.text)
  • 打印结果:
<Response [200]>
200
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>æ–°é—»</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>å
³äºŽç™¾åº¦</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前å¿
读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
import requests

# response = requests.get("http://www.baidu.com")
# print(response)
# print(response.status_code)
# print(response.text)

url = "http://localhost:5000"
# 无参数的get请求
r1 = requests.get("http://localhost:5000")
print(r1.text)
# 带路径的get请求
r2 = requests .get(url + "hello")
print(r2.text)

# 路径上带参数的get请求
r3 = requests.get(url + "hello/我是参数")
print(r3.text)
# 路径上带参数,并且带有参数值得get请求
r4 = requests.get(url + "hello/args/我是参数11?key=我是key&value我是value")
print(r4.text)

# 第二种带参数的方式
r5 = requests .get(url + "hello/args/我是参数222",{"key":"=我是key2","value":"我是value2"})
print(r5.text)
hello world qqqqqqqq
hello22222
hello22222我是参数
hello22222我是参数::::我是key::::我是value
hello22222我是参数::::我是key2::::我是value2

3. requests发送post接口请求

  • 发送post请求:
import requests

url = "http://localhost:5000"
r1 = requests.post(url + "mypost")
print(r1.text)


# 请求数据是表单类型的数据
r2 = requests.post(url + "myposy1",
                   data={
                       "username":,"我是名字",
                        "sex":"我是性别"
                   })
print(r2.text)

# 请求时json类型的数据
r3 = requests.post(url + "mypost2",
                   json={
                       "user":"我是json的user的key",
                       "value":"我是value"
                   })
print(r3.text)
  • 打印结果:
post request
post request我是名字::我是性别
post request{'user':'我是json的user的key','value':'我是value','sex':'男'}

4. requests发送请求头信息

http://111.231.103.117:8083/swagger-ui.html#/

1675853376901.png
一、Requests发送HTTP请求
案例:选定某个测试的URL,利用requests库内置的不同的方法来发送不同类型(GET/POST/PUT/DELETE)的http请求,并输出响应的状态码

image.png

# 导入requests库
import requests

# 定义base_url作为基础被测URL
base_url = 'http://httpbin.org'

# 发送get请求;打印响应状态码
r = requests.get(base_url+'/get')
print(r.status_code)

# 发送POST请求;打印响应状态码
r = requests.post(base_url+'/post')
print(r.status_code)

# 发送PUT请求;打印响应状态码
r = requests.put(base_url+'/put')
print(r.status_code)

# 发送DELETE请求,打印响应状态码
r = requests.delete(base_url+'/delete')
print(r.status_code)

执行结果:
发送4种不同请求,对应响应状态码都是200,请求发送OK

二、Requests参数传递
1.传递URL参数
案例:利用Requests库,在GET请求中使用查询字符串(Query String)传递参数。

# 导入requests库
import requests

# 定义base_url作为基础被测URL
base_url = 'http://httpbin.org'

# 定义请求所需的参数,参数之间以英文逗号隔开
param_data = {'uname':'Test00001','pwd':'123456'}
# 发送GET请求,格式如:requests.get(url,params)
r = requests.get(base_url+'/get',params=param_data)
print(r.url)    # 输出请求的url
print(r.status_code)    #输出响应的状态码

可见查询字符串参数可以使用param进行接收,参数定义为字典格式即可;

  • 2.传递Body参数
    案例:利用Requests库,在POST请求的请求体(Request Body)中传递参数
import requests
base_url = 'http://httpbin.org'

form_data = {'uname':'Test00002','pwd':'123456'}
# 发送POST请求,格式如:requests.post(url,data)
r = requests.post(base_url+'/post',data=form_data)
print(r.text)   # 返回响应内容

body参数在request中使用data接收的情况比较多;

三、Requests请求头设置
1.设置Request Headers
案例:利用Requests库,为POST请求添加HTTP Headers,此时需要传递一个字典类型的数据给headers参数

import requests

base_url = 'http://httpbin.org'

form_data = {'uname':'Test00003','pwd':'123456'}
header = {'user-agent':'Mozilla/5.0'}
r = requests.post(base_url+'/post',data=form_data,headers=header)
print(r.text)  #以文本形式返回响应内容

print(r.text)代表以文本形式返回响应内容

若以上代码修改为print(r.json()),代表以json形式返回响应内容;

2.Request Headers爬虫应用
爬虫程序通过定制Request Headers来模拟浏览器的访问行为,以应对网站的反爬虫策略,避免被封。
1
如:爬取知乎页面元素时,需要设置与浏览器一致的请求头,然后再发送请求
测试url:https://www.zhihu.com/explore
(从浏览器Copy一份User-Agent作为设置的Headers)

image.png

import requests
header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
r = requests.get('https://www.zhihu.com/explore',headers=header)
print(r.text)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

接口测试——requests接口请求(十) 的相关文章

  • 回收销毁,IT界头条2022年7月7日版

    2022年7月 7日 1 关于构建数据基础制度更好发挥数据要素作用的意见 审议通过 2 微软在新报告中揭示了俄乌冲突期间的网络战细节 3 西北工业大学遭受境外网络攻击 xff0c 西安警方已立案侦查 4 立陶宛对俄罗斯 禁运 后遭网络攻击
  • 数据安全与销毁:数据安全已经上升到了国家战略层面

    日前 xff0c 中央全面深化改革委员会审议通过了 关于构建数据基础制度更好发挥数据要素作用的意见 xff08 下称 意见 xff09 xff0c 明确提出 把安全贯穿数据治理全过程 业内专家表示 xff0c 数据安全已经上升到了国家战略层

随机推荐

  • C++用带有默认参数的函数实现,求2个或3个正整数中的最大数

    1 题目要求如下 xff1a C 43 43 用带有默认参数的函数实现 xff0c 求2个或3个正整数中的最大数 2 来吧 xff0c 展示 xff1a include lt iostream gt using namespace std
  • 程序设计思维与实践 Week2 作业B "倒水问题"

    数据 xff1a Sample Input xff1a 2 7 5 2 7 4 Sample Output xff1a fill B pour B A success fill A pour A B fill A pour A B succ
  • armbian的换源

    安装好armbian和众多Linux一样 xff0c 最重要的就是把原来的官方源给替换掉 xff0c 换成国内的源 xff0c 当然个人建议还是把官方的源备份一下以防出错 cp etc apt sources list etc apt so
  • Ubuntu18.04上网断断续续

    刚刚体验了一把Ubuntu18 04 LTS xff0c 有个小问题就是 xff0c 网络链接老是断断续续 后来在这里找到了解决方法 xff1a span class hljs built in sudo span gedit etc pp
  • Coursera Machine Learning 第二周 quiz Octave/Matlab Tutorial 习题答案

    1 Suppose I first execute the following Octave Matlab commands 1 2
  • C语言random问题

    总 结一下C语言random的用法 xff1a srand xff08 xff08 int xff09 time xff08 NULL xff09 xff09 用于设定随机数种子 rand 100 xff0c 产生 0 99 的随机数 如果
  • java.util.regex.PatternSyntaxException

    在处理字符串用到String replaceAll 这个方法的时候出现了这个异常 Exception in thread 34 main 34 java util regex PatternSyntaxException Dangling
  • Shell 脚本 Debug 方法

    可能有的程序员在对程序调试的时候用printf或者echo将信息挨条打印出来 xff0c 但是这比较麻烦 xff0c 因为在交付的时候还要将这些语句一条条删除 xff0c 下面对shell debug的方法稍微做一个总结 xff1a 1 使
  • JAVA 点击按钮展开一个新的Jpanel

    问题不太容易用语言来描述 xff0c 先直接上图吧 xff1a 点击按钮之前 xff1a 点击按钮之后 xff1a 那么如何实现这种功能呢 xff1f 首先在图一中的主JFrame中添加一个JScrollPane xff0c 在点击按钮后n
  • java 实现日历选择器

    首先引用com qt datapicker DatePicker 包实现如下 xff1a package Date import java awt event ActionEvent import java awt event Action
  • 获取JPasswordField组件中的密码

    在JTextField中有一个方法getText xff0c 可以返回组件中输入的字符串 xff0c 但是对于JPasswordField类 xff0c getText 方法已经不适用了 xff0c 执意使用的话 xff0c 获取的也是一串
  • 指针的大小

    说这个之前先了解几个概念 xff1a 字长 xff1a 字长是CPU的主要技术指标之一 xff0c 指的是CPU一次能并行处理的二进制的位数 xff0c 字长是8的整倍数 xff0c 通常的PC机的字长为16位 xff0c 32位 xff0
  • 程序设计思维与实践 Week6 作业A氪金带东树的直径的应用

    题意 xff1a 依次输入图中的点以及边权等信息 xff0c 最后输出每个点在图中所能到达的最远的路线的长度 例如所给的样例 xff1a input 输入文件包含多组测试数据 对于每组测试数据 xff0c 第一行一个整数N N lt 61
  • 《UNIX环境高级编程》(第二版)找不到apue.h问题

    UNIX环境高级编程 xff08 第二版 xff09 这本书 xff0c 实例程序中都包含头文件apue h xff0c 寻找linux usr include中 xff0c 缺找不到此头文件 xff0c 因此编译时会出错 实际上apue
  • java程序中,如何安全的结束一个正在运行的线程?

    如何停止java的线程一直是一个开发多线程程序常遇到的一个问题 在Java的多线程编程中 xff0c java lang Thread类型包含了一些列的方法start stop stop Throwable and suspend dest
  • RoboMaster视觉教程(1)摄像头

    观文有感 之 RoboMaster视觉教程 xff08 1 xff09 摄像头 闲来垂钓碧溪上 今天钓到一篇RM视觉摄像头的好文 xff0c 记录一下笔记 xff1a 文章目录 观文有感 之 RoboMaster视觉教程 xff08 1 x
  • 如何成为一名很酷的机器人工程师

    观文有感 之 如何成为一名很酷的机器人工程师 闲来垂钓碧溪上 今天来钓一波职业规划 xff0c 记录一下笔记 xff08 特别注意 xff1a 本文中大部分内容是复制粘贴的 xff0c 只有少数位置的删改和整理 xff0c 目的是分享一下大
  • 不同层面禁用PUT、DELETE、HEAD、TRACE、OPTIONS请求方式

    背景 对于一些对安全级别要求高的应用 xff0c 可能只允许有GET和POST请求 xff0c 其他请求方式需要禁用 xff0c 那么可以从多个层面来进行禁用 下面从大范围禁用到小范围禁用罗列如下 xff08 假定服务容器是tomcat x
  • keil中出现Undefined symbol FLASH_PrefetchBufferCmd (referred from main.o)等问题解决办法

    在keil中仿照别人的程序写了RCC初始化的程序 xff0c 编译后出现以下问题 obj pro1 axf Error L6218E Undefined symbol FLASH PrefetchBufferCmd referred fro
  • 接口测试——requests接口请求(十)

    1 requests库介绍与安装 requests库介绍 requests是一款非常火爆且常用的Python三方库能够实现HTTP协议的各种请求方法使用简单易上手 requests库的安装方法 pip install requests安装成