The get and post, packaged together, depending on the request method, the method of determining the call. When calling a call on it.
Import Requests Import JSON class Interface (): '' ' Exercise Test Interface ' '' DEF send_post (Self, URL, Data): '' ' package post requests ' '' Result = requests.post (URL, Data) ASSIGN = Result .json () return json.dumps (ASSIGN, indent =. 4, sort_keys = True, ensure_ascii = False) DEF send_get (Self, URL, Data): '' ' packages get request ' '' Result = requests.get (URL, Data) ASSIGN = Result. json() return json.dumps (ASSIGN, indent =. 4, sort_keys = True, ensure_ascii = False) DEF run_main (Self, URL, Method, Data = None): '' ' to perform the method invocation request mode ' '' RES = None IF Method == ' the GET ' : RES = self.send_get (URL, Data = None) the else : RES = self.send_post (URL, Data) return RES IF the __name__ == ' __main__ ' : '' ' call execution ' '' api = Interface() url = 'https://api.apishop.net/common/weather/get15DaysWeatherByArea' data = { "apiKey": 'chgaxvsf88f3858a15fa4426f4cbdd4d2a02b92ee0747f3', "area": '重庆', } print(api.run_main(url, 'POST',data))
Relatively easy to understand wording:
import requests import json def send_post(url,data): '''封装post请求''' result = requests.post(url,data) assign = result.json() return json.dumps(assign,indent=4,sort_keys=True,ensure_ascii=False) def send_get(url,data): '''封装get请求''' result = requests.get(url,data) assign = result.json() return json.dumps(assign,indent=4,sort_keys=True,ensure_ascii=False) def main(url,method,data=None): '' ' to perform the method invocation request mode ' '' IF Method == ' the GET ' : return send_get (URL, Data) the else : return send_post (URL, Data) URL = ' HTTPS: //api.apishop. NET / Common / Weather / get15DaysWeatherByArea ' Data = { " the apiKey " : ' chgaxvsf88f3858a15fa4426f4cbdd4d2a02b92ee0747f3 " , " Area " : ' Chongqing ' , } #url = 'https://mgapp.sitezt.cn/api/info/mgapp/index/getindexbanner' print(main(url,'POST',data))