import requests
print("===============get请求================")
url = 'http://api.nnzhp.cn/api/user/stu_info'
data = {'stu_name':'小黑'} #请求数据
req = requests.get(url=url,params=data)
req = requests.get(url,params=data) #params:默认值参数,可传值,也可不传值;url:位置参数,必须传值
print(req.json(),'====>',type(req)) #返回的是一个字典类型
print(req.text) #返回一个Json格式的字符串类型
print("==============post请求==================")
url = 'http://api.nnzhp.cn/api/user/login'
data = {'username':'fanshaoyan','passwd':'Aa123456'} #请求数据
req = requests.post(url,data)
print(req.text,'====>',type(req)) #返回一个Json格式的字符串类型
print(req.json(),'====>',type(req)) #返回的是一个字典类型
print("===============入参是json类型===============")
import random
phone=random.randint(10000000000,99999999999) #随机产生一个11位的数字
url='http://api.nnzhp.cn/api/user/add_stu'
data ={
"name":"臭臭虫",
"grade":"天蝎座",
"phone":phone,
"sex":"女",
"age":28,
"addr":"河南省济源市北海大道32号"
}
req = requests.post(url,json=data)
print(req.json())
print("==============添加cookie===============")
url = 'http://api.nnzhp.cn/api/user/gold_add'
data = {'stu_id':468,'gold':10000}
cook = {'niuhanyang':'337ca4cc825302b3a8791ac7f9dc4bc6'}
req = requests.post(url,data,cookies=cook)
print(req.json(),'====>',type(req))
print("================添加header===============")
url = 'http://api.nnzhp.cn/api/user/all_stu'
header = { 'Referer':'http://api.nnzhp.cn/' }
req = requests.get(url,headers=header)
print(req.json(),type(req))
print("===============上传文件===================")
url= 'http://api.nnzhp.cn/api/file/file_upload'
data = {
'file':open(r'D:\电脑桌面.jpg','rb')
}
req= requests.post(url,files=data)
print(req.json())
print("=================下载文件==================")
url = 'http://up.mcyt.net/?down/46779.mp3'
req = requests.get(url)
fw = open('离别的车站.mp3','wb')
fw.write(req.content) #req.content 是 bytes(二进制)类型