Python接口请求及封装

基于http协议,最常用的是GETPOST两种方法。

接口文档需要包含哪些信息:

接口名称
接口功能
接口地址
支持格式 json/xml
请求方式
请求示例
请求参数(是否必填、数据类型、传递参数格式)
返回参数说明
以典型的(一两个)参数做为判断是否请求通过(重点是看响应的信息判断)

一、GET

 1 import requests
 2 import json
 3 
 4 url = "http://v.juhe.cn/laohuangli/d"
 5 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}
 6 header ={}
 7 
 8 r = requests.get(url,params=para,headers= header)
 9 
10 print('get请求获取的响应结果json类型',r.text)
11 print("get请求获取响应状态码",r.status_code)
12 print("get请求获取响应头",r.headers['Content-Type'])
13 
14 #响应的json数据转换为可被python识别的数据类型
15 json_r = r.json()
16 print(json_r)

二、POST

post请求有两种请求格式:
1、key-value的格式'Content-Type':'application/x-www-form-urlencoded'
2、标准json的格式:'Content-Type':'application/json'

#key-value

 1 import requests
 2 import json
 3 
 4 url = "http://v.juhe.cn/laohuangli/d"
 5 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}
 6 header ={}
 7 
 8 r = requests.post(url,data=para,headers= header)
 9 
10 print('get请求获取的响应结果json类型',r.text)
11 print("get请求获取响应状态码",r.status_code)
12 print("get请求获取响应头",r.headers['Content-Type'])
13 
14 #响应的json数据转换为可被python识别的数据类型
15 json_r = r.json()
16 print(json_r)

#json

 1 import requests
 2 import json
 3 
 4 url = "http://v.juhe.cn/laohuangli/d"
 5 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}
 6 header ={}
 7 #python数据类型转换为json类型(json.dumps())
 8 para = json.dumps(para)
 9 r = requests.post(url,data=para,headers= header)
10 
11 print('get请求获取的响应结果json类型',r.text)
12 print("get请求获取响应状态码",r.status_code)
13 print("get请求获取响应头",r.headers['Content-Type'])
14 
15 #响应的json数据转换为可被python识别的数据类型
16 json_r = r.json()
17 print(json_r)

三、把所有的请求封装在函数中

 1 def get(url,para,headers):
 2     try:
 3         r = requests.get(url,params=para,headers=headers)
 4         print("获取返回的状态码",r.status_code)
 5         json_r = r.json()
 6         print("json类型转化成python数据类型",json_r)
 7     except BaseException as e:
 8         print("请求失败!",str(e))
 9 def post(url,para,headers):
10     try:
11         r = requests.post(url,data=para,headers=headers)
12         print("获取返回的状态码",r.status_code)
13         json_r = r.json()
14         print("json类型转化成python数据类型",json_r)
15     except BaseException as e:
16         print("请求失败!",str(e))
17 def post_json(url,para,headers):
18     try:
19         data = para
20         data = json.dumps(data)   #python数据类型转化为json数据类型
21         r = requests.post(url,data=data,headers=headers)
22         print("获取返回的状态码",r.status_code)
23         json_r = r.json()
24         print("json转换为python数据类型:",json_r)
25     except BaseException as e:
26         print("请求失败!",str(e))
27 
28 url = "http://v.juhe.cn/laohuangli/d"
29 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}
30 headers ={}
31 
32 get(url,para,headers)
33 post(url,para,headers)
34 post_json(url,para,headers)

四、把所有请求封装在一个对象里

 1 class Webrequests:
 2     def get(self,url,para,headers):
 3         try:
 4             r = requests.get(url,params=para,headers=headers)
 5             print("获取返回的状态码",r.status_code)
 6             json_r = r.json()
 7             print("json类型转化成python数据类型",json_r)
 8         except BaseException as e:
 9             print("请求失败!",str(e))
10     def post(self,url,para,headers):
11         try:
12             r = requests.post(url,data=para,headers=headers)
13             print("获取返回的状态码",r.status_code)
14             json_r = r.json()
15             print("json类型转化成python数据类型",json_r)
16         except BaseException as e:
17             print("请求失败!",str(e))
18     def post_json(self,url,para,headers):
19         try:
20             data = para
21             data = json.dumps(data)   #python数据类型转化为json数据类型
22             r = requests.post(url,data=data,headers=headers)
23             print("获取返回的状态码",r.status_code)
24             json_r = r.json()
25             print("json类型转化成python数据类型",json_r)
26         except BaseException as e:
27             print("请求失败!",str(e))
28 
29 url = "http://v.juhe.cn/laohuangli/d"
30 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}
31 headers ={}
32 
33 q = Webrequests()
34 
35 q.get(url,para,headers)
36 q.post(url,para,headers)
37 q.post_json(url,para,headers)
posted @ 2017-12-21 16:45  海布里Simple  阅读(2620)  评论(0编辑  收藏  举报