前言基于http协议,最常用的是GET和POST两种方法 。
接口文档需要包含哪些信息:
- 接口名称
- 接口功能
- 接口地址
- 支持格式 json/xml
- 请求方式
- 请求示例
- 请求参数(是否必填、数据类型、传递参数格式)
- 返回参数说明
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)
#jsonimport 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)
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- vue项目,实现页面局部刷新。非window.location.方式
- 耳机|大神魔改AirPods:不但换壳 还有USB-C接口
- 遗传算法的基本概念和实现 遗传算法
- AV RCA接口是什么 rca接口是什么意思
- 用户态协议栈设计实现udp,arp与icmp协议
- 数据库读写分离详解
- 基于阿里云实现WEB访问故障转移静态提示页面
- Mac实现剪切的功能 剪切的快捷键
- 权责发生制和收付实现制两者的区别何在? 权责发生制
- 牛肉|想吃肉怎么办,学会这两道菜,实现吃肉自由,简单营养美味