requests模块

import requests,random
#发get请求
# url='http://api.nnzhp.cn/api/user/stu_info'
# data={'stu_name':'小黑'}#请求数据
# req=requests.get(url,params=data)#发get请求
# print(req.json())#字典
# print(req.text)#string,json串
#返回的都是什么
#返回的类型是什么
#2、post请求
# url='http://api.nnzhp.cn/api/user/login'
# data={'username':'niuhanyang','passwd':'aA123456'}#请求数据
# req=requests.post(url,data)#发送post请求
# print(req.json())
#3、入参是json类型
# url='http://api.nnzhp.cn/api/user/add_stu'
# phone=random.randint(10000000000,99999999999)
# data={
# "name": "小7",
# "grade": "天蝎座",
# "phone": phone,
# "sex": "男",
# "age": 28,
# "addr": "河南省济源市北海大道32号"
# }
# req=requests.post(url,json=data)#入参是json类型直接写json=
# print(req.json())
#4、添加cookie
# url='http://api.nnzhp.cn/api/user/gold_add'
# data={'stu_id':468,'gold':1000}
# cookie={'niuhanyang':'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('/Users/zyp/Downloads/2cf4c891827837341b31202fb6f4a92d.jpeg','rb')
# }
# req=requests.post(url,files=data)
# print(req.json())
#下载一个文件,或者下图片
#7、下载文件,图片.jpg,音乐.mp3,网址.html
# url='http://www.nnzhp.cn/wp-content/uploads/2018/01/soup.jpg'
# req=requests.get(url)
# # req.content#这个content返回的二进制的
# fw=open('s.jpg','wb')#二进制写模式
# fw.write(req.content)

url='http://up.mcyt.net/?down/46779.mp3'
req=requests.get(url)
# req.content#这个content返回的二进制的
fw=open('music.mp3','wb')#二进制写模式 网站都是html
fw.write(req.content)

posted on 2018-05-15 16:23  公子兔  阅读(116)  评论(0编辑  收藏  举报

导航