import requests # #发送get请求 # url='http://api.nnzhp.cn/api/user/stu_info' # data={'stu_name':'小黑'} # req=requests.get(url,params=data) # print(req.json())#返回值是字典 单引号 # print(req.text)#返回值是string,json串 双引号 #这是post请求,需要传参 # url='http://api.nnzhp.cn/api/user/login' # data={'username':'niuhanyang','passwd':'aA123456'} # req=requests.post(url,data) # print(req.json()) # #入参是json类型的 # import random # phone=random.randint(1000000000,999999999999999) # url='http://api.nnzhp.cn/api/user/add_stu' # data={ # 'name':'fyr', # 'grade':'1班', # 'phone':phone, # 'sex':'男', # 'age':28, # 'addr':'北京' # } # req=requests.post(url,data) # #4、入参是cookie # url='http://api.nnzhp.cn/api/user/gold_add' # data={ # 'stu_id':471,'gold':1000 # } # cookie={'nhy':'337ca4cc825302b3a8791ac7f9dc4bc6'} # req=requests.post(url,data,cookies=cookie) # print(req.json()) # #5、添加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()) # #6、入参是文件 # url='http://api.nnzhp.cn/api/file/file_upload' # data={ # 'file':open('搭建测试环境',encoding='utf-8') # } # req=requests.post(url,files=data) # print(req.json()) #7、下载一个文件或者下载一个图片从网上 url='http://www.nnzhp.cn/wp-content/uploads/2018/01/soup.jpg' req=requests.get(url) req.content#返回的是二进制的文件 print(req.content) fw=open('a.jpg','wb')#wb是写二进制 fw.write(req.content)