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


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


  • 相关阅读:
    H面试程序(0):字符串一些常用函数的实现
    ctype.h头文件
    poj 3657
    UVA10294项链和手镯(等价类计数问题)
    UVA11375火柴(递推+大数)
    UVA11375火柴(递推+大数)
    UVA11388GCD LCM
    UVA11388GCD LCM
    UVA10943简单递推
    UVA10943简单递推
  • 原文地址:https://www.cnblogs.com/JacquelineQA/p/12677045.html
Copyright © 2020-2023  润新知