码迷,mamicode.com
首页 > 其他好文 > 详细

request请求的get/post的模块的封装

时间:2020-04-11 20:19:54      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:post   登录   mobile   phone   method   cookie   pre   添加   mon   

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 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/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())


request请求的get/post的模块的封装

标签:post   登录   mobile   phone   method   cookie   pre   添加   mon   

原文地址:https://www.cnblogs.com/JacquelineQA/p/12677045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!