Python实现接口请求及封装

前言基于http协议,最常用的是GET和POST两种方法 。
接口文档需要包含哪些信息:

  1. 接口名称
  2. 接口功能
  3. 接口地址
  4. 支持格式 json/xml
  5. 请求方式
  6. 请求示例
  7. 请求参数(是否必填、数据类型、传递参数格式)
  8. 返回参数说明
以典型的(一两个)参数做为判断是否请求通过(重点是看响应的信息判断)
GET请求import requestsimport jsonurl = "http://v.juhe.cn/laohuangli/d"para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}header ={}r = requests.get(url,params=para,headers= header,)#verify=True适用于服务端的ssl证书验证,verify=False为关闭ssl验证print('get请求获取的响应结果json类型',r.text) print("get请求获取响应状态码",r.status_code) print("get请求获取响应头",r.headers['Content-Type']) #响应的json数据转换为可被Python/ target=_blank class=infotextkey>Python识别的数据类型json_r = r.json() print(json_r)POST请求post请求有两种常用的请求格式:
【Python实现接口请求及封装】1、key-value的格式'Content-Type':'
Application/x-www-form-urlencoded'
2、标准json的格式:'Content-Type':'application/json'
#key-value
import requestsimport jsonurl = "http://v.juhe.cn/laohuangli/d"para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}header ={}r = requests.post(url,data=https://www.isolves.com/it/cxkf/yy/Python/2022-07-22/para,headers= header)print('get请求获取的响应结果json类型',r.text)print("get请求获取响应状态码",r.status_code)print("get请求获取响应头",r.headers['Content-Type'])#响应的json数据转换为可被python识别的数据类型json_r = r.json()print(json_r)#json
import requestsimport jsonurl = "http://v.juhe.cn/laohuangli/d"para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}header ={}#python数据类型转换为json类型(json.dumps())para = json.dumps(para)r = requests.post(url,data=https://www.isolves.com/it/cxkf/yy/Python/2022-07-22/para,headers= header)print('get请求获取的响应结果json类型',r.text)print("get请求获取响应状态码",r.status_code)print("get请求获取响应头",r.headers['Content-Type'])#响应的json数据转换为可被python识别的数据类型json_r = r.json()print(json_r)将请求进行封装1、把所有的请求封装在函数中
def get(url,para,headers):try:r = requests.get(url,params=para,headers=headers)print("获取返回的状态码",r.status_code)json_r = r.json()print("json类型转化成python数据类型",json_r)except BaseException as e:print("请求失败!",str(e))def post(url,para,headers):try:r = requests.post(url,data=https://www.isolves.com/it/cxkf/yy/Python/2022-07-22/para,headers=headers)print("获取返回的状态码",r.status_code)json_r = r.json()print("json类型转化成python数据类型",json_r)except BaseException as e:print("请求失败!",str(e))def post_json(url,para,headers):try:data = paradata = json.dumps(data)#python数据类型转化为json数据类型r = requests.post(url,data=data,headers=headers)print("获取返回的状态码",r.status_code)json_r = r.json()print("json转换为python数据类型:",json_r)except BaseException as e:print("请求失败!",str(e))url = "http://v.juhe.cn/laohuangli/d"para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}headers ={}get(url,para,headers)post(url,para,headers)post_json(url,para,headers)2、把所有请求封装在一个对象里
class Webrequests:def get(self,url,para,headers):try:r = requests.get(url,params=para,headers=headers)print("获取返回的状态码",r.status_code)json_r = r.json()print("json类型转化成python数据类型",json_r)except BaseException as e:print("请求失败!",str(e))def post(self,url,para,headers):try:r = requests.post(url,data=https://www.isolves.com/it/cxkf/yy/Python/2022-07-22/para,headers=headers)print("获取返回的状态码",r.status_code)json_r = r.json()print("json类型转化成python数据类型",json_r)except BaseException as e:print("请求失败!",str(e))def post_json(self,url,para,headers):try:data = paradata = json.dumps(data)#python数据类型转化为json数据类型r = requests.post(url,data=data,headers=headers)print("获取返回的状态码",r.status_code)json_r = r.json()print("json类型转化成python数据类型",json_r)except BaseException as e:print("请求失败!",str(e))url = "http://v.juhe.cn/laohuangli/d"para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}headers ={}q = Webrequests()q.get(url,para,headers)q.post(url,para,headers)q.post_json(url,para,headers)


推荐阅读