基於http協議,最經常使用的是GET和POST兩種方法。python
接口文檔須要包含哪些信息:json
接口名稱
接口功能
接口地址
支持格式 json/xml
請求方式
請求示例
請求參數(是否必填、數據類型、傳遞參數格式)
返回參數說明
以典型的(一兩個)參數作爲判斷是否請求經過(重點是看響應的信息判斷)app
1、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)
2、POSTpost
post請求有兩種請求格式:
一、key-value的格式'Content-Type':'application/x-www-form-urlencoded'
二、標準json的格式:'Content-Type':'application/json'url
#key-valuespa
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)
#jsoncode
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)
3、把全部的請求封裝在函數中orm
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)
4、把全部請求封裝在一個對象裏xml
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)