【示例】requests库基础使用教程

目录

代码编写套路

requests对象-发送接口请求

示例一-百度

示例二get-params

示例三post-form

示例四post-json

示例五-put

示例六-delete


代码编写套路

  • 导入requests库
  • 准备测试数据
  • 发送请求数据,获取响应对象
  • 查看响应结果

requests对象-发送接口请求

requests.请求方法(url,params=None,data=None,json=None,headers=None,cookies=None)

示例一-百度

#1. 导入requests库
import requests

#2. 准备测试数据
req_url = "http://www.baidu.com"

#3. 发送请求数据,获取响应对象
resp = requests.get(url=req_url)   # 传递接口的url给get方法

#4. 查看响应结果
print(resp.status_code)   # 打印状态码
print(resp.text)   # 打印响应文本数据

示例二get-params

# 1-导入requests包
import requests
# 2-定义请求数据(请求url、请求数据)
req_url = "http://hmshop-test.itheima.net/Home/Goods/search.html"
req_params = {"q": "iphone"}
# 3-发送接口请求,获取响应对象
resp = requests.get(url=req_url,params=req_params) # 传递url和查询参数
# # 请求方式二【了解】
# req_url = "http://hmshop-test.itheima.net/Home/Goods/search.html?q=iphone"
# resp = requests.get(url=req_url)
# 4-获取响应的数据
print(resp.status_code)
print(resp.text)

示例三post-form

# 1-导入requests包
import requests
# 2-定义请求数据
req_url = "http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=do_login"
req_data = {"username": "13800000002", "password": "123456", "verify_code": "8888"}
req_head = {"Content-Type":"application/x-www-form-urlencoded"} # 定义请求头,可选,可以不传,
requests会根据data传参自动处理
# 3-执行接口请求,获取响应对象
resp = requests.post(url=req_url,data=req_data,headers=req_head) # data传递表单数据 headers
传递请求头
# 4-获取响应的结果
print(resp.status_code)
print(resp.text)
print(resp.json()) # 只有返回数据是json格式才能使用,否则会报错

示例四post-json

# 1-导入requests库
import requests
# 2-定义请求数据(url、请求数据)
req_url = "http://ihrm2-test.itheima.net/api/sys/login"
req_json = {"mobile":"13800000002", "password":"123456"}
req_head = {"Content-Type":"application/json"} # 可省略,requests库会根据你传参的方式自动处理
# 3-发送接口请求,获取响应对象
resp = requests.post(url=req_url,json=req_json,headers=req_head) # 通过json参数传递json数据
# 4-打印响应结果(状态码、响应数据)
print(resp.status_code)
print(resp.json(),type(resp.json()))

示例五-put

# 1-导入requests库
import requests
# 2-定义测试的数据(url、请求数据、请求头[令牌])
req_url = "http://ihrm2-test.itheima.net/api/sys/user/1648307686900510720"
req_json = {"username": "Amy", "password":"456789"}
req_head = {"Content-Type":"application/json","Authorization":"Bearer bab51233-69b3-48ca9464-6f133111c5f4"}
# 3-发送接口请求,获取响应对象
resp = requests.put(url=req_url,json=req_json,headers=req_head) # 添加令牌,处理认证信息
# 4-打印响应的结果
print(resp.status_code)
print(resp.json())

示例六-delete

# requests套路:
# 1-导入requests库
import requests
# 2-准备测试数据
req_url = "http://ihrm2-test.itheima.net/api/sys/user/1647208038832025600"
req_head = {"Authorization": "Bearer 6e9c5d80-ba87-4db2-8a94-0596e9eb1e81"}
# 3-发送接口请求,获取响应对象
resp = requests.delete(url=req_url,headers=req_head)
# 4-查看响应结果
print(resp.text)
print(resp.json()) # 当响应数据的格式是json的时候才能用这个方法,否则报错

  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值