get是登陸請求,post是充值請求,須要登陸的cookie.
#利用requests封裝get和post請求
import requests
#不管是get,post請求都須要url,data(傳遞參數,mobilephone,pwd,amount),cookies,get裏沒有cookies,
# 咱們在response裏添加一個cookie=None,這樣get和post請求格式和內容就差很少類似,
# 建立一個類class
class HttpRequest:
def http_request(self,url,data,method,cookie=None):
#url,data,cookis均可以參數化
if method=='get':
res = requests.get(url, data, cookies=cookie)
else:
res=requests.post(url,data,cookies=cookie)
print("響應正文: ",res.json())
return res
if __name__ == '__main__':
url = 'http://test.lemonban.com/futureloan/mvc/api/member/login'
data = {"mobilephone": "18688773467", "pwd": "123456"}
res=HttpRequest().http_request(url,data,'post')#把url,data傳進來,cookie默認爲none
print("登陸結果是: ", res.json())
#充值
recharge_url='http://test.lemonban.com/futureloan/mvc/api/member/recharge'
recharge_data={"mobilephone":"18688773467","amount":"1000"}
recharge_res = HttpRequest().http_request(recharge_url,recharge_data,'get',res.cookies)
print("充值結果是: ", recharge_res.json())
#https 請求:request.get(url,data,verify=false)
找了其餘網站練習
import requests
class HttpRequest:
def http_request(self,url,data,method,cookie=None):
if method=='get':
res = requests.get(url, data, cookies=cookie)
else:
res=requests.post(url,data,cookies=cookie)
print("響應正文: ",res.json())
return res
if __name__ == '__main__':
url = 'http://v.juhe.cn/laohuangli/d'
data = {"key": "XXXXX", "date": "2020-4-11"}
res=HttpRequest().http_request(url,data,'post')
print("post結果是: ", res.json())
get_res = HttpRequest().http_request(url, data, 'get', res.cookies)
print("get結果是: ", res.json())
另一個網站
import requestsclass HttpRequest: def http_request(self,url,data,method,cookie=None): if method=='get': res = requests.get(url, data, cookies=cookie) else: res=requests.post(url,data,cookies=cookie) print("響應正文: ",res.json()) return resif __name__ == '__main__': url = 'http://v.juhe.cn/toutiao/index' data = {"key": "XXXXXXX ", "type": "guonei"} res=HttpRequest().http_request(url,data,'post') print("post結果是: ", res.json()) get_res = HttpRequest().http_request(url, data, 'get', res.cookies) print("get結果是: ", res.json())