基于http协议,最常用的是GET和POST两种方法。
接口文档需要包含哪些信息:
以典型的(一两个)参数做为判断是否请求通过(重点是看响应的信息判断)
import requests
import json
url = "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请求有两种常用的请求格式:
1、key-value的格式'Content-Type':'
Application/x-www-form-urlencoded'
2、标准json的格式:'Content-Type':'application/json'
#key-value
import requests
import json
url = "http://v.juhe.cn/laohuangli/d"
para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"}
header ={}
r = requests.post(url,data=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 requests
import json
url = "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=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=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 = para
data = 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=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 = para
data = 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)