zoukankan      html  css  js  c++  java
  • Python第一个请求接口

    1.普通get请求

    import requests
    import json
    login_res=requests.post(url='http://joy.web.com:8090/login',data={"username": "joy","password": "joypawd"})
    
    # 返回字符串类型的文本内容
    print(login_res.text)
    
    # 返回字典类型的json格式内容(null返回None)
    print(login_res.json())
    
    # 返回cookies信息
    print(login_res.cookies)
    
    # 返回字节流(破解)
    print(login_res.content.decode('utf8'))
    
    # 返回字节流
    print(login_res.content)
    
    # 返回响应头
    print(login_res.headers)
    
    # 返回状态码
    print(login_res.status_code)
    

    2.json格式请求

    import requests
    import json
    
    
    url = "https://cp.joy.com.cn/api/get"
    values = {'Id':'n6085MMV','userId': '89','location':'深圳市'}
    # 打印values的数据类型,输出<class 'dict'>
    print(type(values))
    print(values)
    # json.dump将python对象编码成json字符串
    values_json = json.dumps(values)
    # 打印编码成json字符串的values_json的数据类型,输出<class 'str'>
    print(type(values_json))
    print(values_json)
    # requests库提交数据进行post请求
    req = requests.post(url, data=values_json,headers={'Content-Type': 'application/json'})
    # 打印Unicode编码格式的json数据
    print(req.text)
    # 使用json.dumps()时需要对象相应的类型是json可序列化的
    change = req.json()
    # json.dumps序列化时对中文默认使用ASCII编码,如果无任何配置则打印的均为ascii字符,输出中文需要指定ensure_ascii=False
    new_req = json.dumps(change, ensure_ascii=False)
    # 打印接口返回的数据,且以中文编码
    print(new_req)
    

      

     
  • 相关阅读:
    .Net环境下的缓存技术介绍
    JavaScript 全局对象
    JavaScript escape() 函数
    实现DIV拖动
    巧用Ajax的beforeSend 提高用户体验
    js中ie8下不识别indexOf的解决办法
    页面弹窗效果HTML
    让html页面中的文字不可选中
    MVC路由规则
    C# Math.Round
  • 原文地址:https://www.cnblogs.com/joy-sir/p/12187242.html
Copyright © 2011-2022 走看看