前言
基于http協(xié)議,最常用的是GET和POST兩種方法。
接口文檔需要包含哪些信息:
- 接口名稱
- 接口功能
- 接口地址
- 支持格式 json/xml
- 請求方式
- 請求示例
- 請求參數(shù)(是否必填、數(shù)據(jù)類型、傳遞參數(shù)格式)
- 返回參數(shù)說明
以典型的(一兩個(gè))參數(shù)做為判斷是否請求通過(重點(diǎn)是看響應(yīng)的信息判斷)
GET請求
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適用于服務(wù)端的ssl證書驗(yàn)證,verify=False為關(guān)閉ssl驗(yàn)證
print('get請求獲取的響應(yīng)結(jié)果json類型',r.text)
print("get請求獲取響應(yīng)狀態(tài)碼",r.status_code)
print("get請求獲取響應(yīng)頭",r.headers['Content-Type'])
#響應(yīng)的json數(shù)據(jù)轉(zhuǎn)換為可被Python/ target=_blank class=infotextkey>Python識(shí)別的數(shù)據(jù)類型
json_r = r.json()
print(json_r)
POST請求
post請求有兩種常用的請求格式:
1、key-value的格式'Content-Type':'
Application/x-www-form-urlencoded'
2、標(biāo)準(zhǔn)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請求獲取的響應(yīng)結(jié)果json類型',r.text)
print("get請求獲取響應(yīng)狀態(tài)碼",r.status_code)
print("get請求獲取響應(yīng)頭",r.headers['Content-Type'])
#響應(yīng)的json數(shù)據(jù)轉(zhuǎn)換為可被python識(shí)別的數(shù)據(jù)類型
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數(shù)據(jù)類型轉(zhuǎn)換為json類型(json.dumps())
para = json.dumps(para)
r = requests.post(url,data=para,headers= header)
print('get請求獲取的響應(yīng)結(jié)果json類型',r.text)
print("get請求獲取響應(yīng)狀態(tài)碼",r.status_code)
print("get請求獲取響應(yīng)頭",r.headers['Content-Type'])
#響應(yīng)的json數(shù)據(jù)轉(zhuǎn)換為可被python識(shí)別的數(shù)據(jù)類型
json_r = r.json()
print(json_r)
將請求進(jìn)行封裝
1、把所有的請求封裝在函數(shù)中
def get(url,para,headers):
try:
r = requests.get(url,params=para,headers=headers)
print("獲取返回的狀態(tài)碼",r.status_code)
json_r = r.json()
print("json類型轉(zhuǎn)化成python數(shù)據(jù)類型",json_r)
except BaseException as e:
print("請求失敗!",str(e))
def post(url,para,headers):
try:
r = requests.post(url,data=para,headers=headers)
print("獲取返回的狀態(tài)碼",r.status_code)
json_r = r.json()
print("json類型轉(zhuǎn)化成python數(shù)據(jù)類型",json_r)
except BaseException as e:
print("請求失敗!",str(e))
def post_json(url,para,headers):
try:
data = para
data = json.dumps(data) #python數(shù)據(jù)類型轉(zhuǎn)化為json數(shù)據(jù)類型
r = requests.post(url,data=data,headers=headers)
print("獲取返回的狀態(tài)碼",r.status_code)
json_r = r.json()
print("json轉(zhuǎn)換為python數(shù)據(jù)類型:",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、把所有請求封裝在一個(gè)對象里
class Webrequests:
def get(self,url,para,headers):
try:
r = requests.get(url,params=para,headers=headers)
print("獲取返回的狀態(tài)碼",r.status_code)
json_r = r.json()
print("json類型轉(zhuǎn)化成python數(shù)據(jù)類型",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("獲取返回的狀態(tài)碼",r.status_code)
json_r = r.json()
print("json類型轉(zhuǎn)化成python數(shù)據(jù)類型",json_r)
except BaseException as e:
print("請求失敗!",str(e))
def post_json(self,url,para,headers):
try:
data = para
data = json.dumps(data) #python數(shù)據(jù)類型轉(zhuǎn)化為json數(shù)據(jù)類型
r = requests.post(url,data=data,headers=headers)
print("獲取返回的狀態(tài)碼",r.status_code)
json_r = r.json()
print("json類型轉(zhuǎn)化成python數(shù)據(jù)類型",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)