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
原文地址:https://www.cnblogs.com/wenchengqingfeng/p/9455897.html