import requests
# 一次实例化多次调用
class HTTPRequest():
cookie = None
def do_request(self,method, url, data =None,headers= None):
method = method.upper()
resp = None
if method == "POST":
resp = requests.post(url=url, data=data, headers=headers, cookies= self.cookie)
elif method == "GET":
resp = requests.get(url=url, params=data, headers=headers, cookies = self.cookie)
else:
print("不支持该请求方式:{}".format(method))
if resp.cookies: # 判断是否有cookies,有就存放在类属性里面
self.cookie = resp.cookies
print(self.cookie)
return resp
if __name__ == '__main__':
# 可将前一个接口填写有cookie的url(登录),看是否会传给下一个接口
method = "get"
url = "填入自己的请求url"
data = {"mobilephone": "15873731233", "pwd": "123456"}
request = HTTPRequest()
respon= request.do_request(method,url,data)
print(respon.text)
method = "post"
url = "填入自己的请求url"
data = {"mobilephone":"15810447878","amount":"100"}
respon = request.do_request(method, url, data)
print(respon.text)