zoukankan      html  css  js  c++  java
  • python------接口(get请求、post请求), 图片、歌曲下载,网页返回,上传文件

    接口(get请求、post请求 --json), 图片、歌曲下载(context),网页返回(text),上传文件
     
    import requests
    ################################接口返回json类型数据##############################
    #带参数get请求
    url='http://118.24.3.40/api/user/stu_info'
    params={'stu_name':'小黑'}
    res=requests.get(url=url,params=params).json()


    #post请求 参数k-v
    url='http://118.24.3.40/api/user/login'
    data={'username':'niuhanyang','passwd':'aA123456'}
    res=requests.post(url=url,data=data).json()

    #post请求 参数json
    url='http://118.24.3.40/api/user/add_stu'
    data={"name":"requests_name",
          "grade":"天蝎座",
          "phone":13811087876,
          "sex":"男",
          "age":28,
          "addr":"天蝎座"
          }
    res=requests.post(url=url,json=data).json()

    #post请求 带cookie

    url='http://118.24.3.40/api/user/gold_add'
    data={"stu_if":"requests_name","gold":200}
    cookie = {'niuhanyang':'abd9a0995f4696e1a60133220b32037a'}
    res=requests.post(url,data=data,cookies=cookie).json()


    #get请求 带header
    url4='http://118.24.3.40/api/user/all_stu'
    header={"Referer":"http://api.nnzhp.cn"}
    res=requests.get(url4,headers=header).json()
    print(res)

    ########################################返回二进制格式数据(图片歌曲等)##########################
    url6='http://qiniuuwmp3.changba.com/1084511584.mp3'
    res=requests.get(url6).content

    #写二进制文件 wb
    with open('歌曲.mp3','wb') as fw:
        fw.write(res.content)

    ########################################返回字符串格式数据(网页等)##########################
    url5='http://www.nnzhp.cn'
    res=requests.get(url5).text #返回的是字符串

    ########################################上传文件##########################
    #post请求上传文件
    #1、post请求 2、参数名data 3、open用rb形式 4、返回内容用content
    url8='http://118.24.3.40/api/file/file_upload'
    data={'file':open('歌曲.mp3','rb')}
    res=requests.post(url8,files=data).content
  • 相关阅读:
    jQuery
    jquery操作滚动条滚动到指定元素位置 scrollTop
    javascript的offset、client、scroll使用方法
    js javascript:void(0) 真正含义
    Js中数组Array的用法
    javascript中typeof、undefined 和 null
    函数的参数是函数,函数中Ajax返回的回调函数中的函数运行
    xampp中php手动升级
    jQuery Mobile里xxx怎么用呀? (事件篇)
    eval解析JSON中的注意点
  • 原文地址:https://www.cnblogs.com/wenchengqingfeng/p/9455897.html
Copyright © 2011-2022 走看看