import requests class MyRequests: # 初始化方法 def __init__(self): # 请求头 self.headers = {"X-Lemonban-Media-Type": "lemonban.v2"} # 属性 # 方法 post/put.. json=XXX , get.. params=XXX def send_requests(self,method,url, data,token=None): # 处理请求头 self.__deal_header(token) # 调用requests的方法去发起一个请求。并得到响应结果 if method.upper() == "GET": resp = requests.request(method, url, params=data, headers=self.headers) else: resp = requests.request(method, url, json=data, headers=self.headers) return resp def __deal_header(self,token=None): if token: self.headers["Authorization"] = "Bearer {}".format(token) if __name__ == '__main__': mr = MyRequests() url = "http://api.lemonban.com/futureloan/member/register" req_data = { "mobile_phone": "18610100322", "pwd": "123456789", "reg_name": "py37小简" } method = "post" resp = mr.send_requests(method,url,req_data) print(resp.json())
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:爬虫示例 - Python技术站