import requests
# 1、发get请求
url='http://api.xxx.xx/api/user/sxx_info'
data={'stu_name':'xxx'}
req=requests.get(url,params=data) #发get请求
print(req.json()) #字典
print(req.text) #string,json串
# 返回的都是什么
# 返回的类型是什么
# 中文的好使吗
# 2、发请求post
url='http://api.xxx.cn/api/user/lxxxn'
data={'username':'xxx','passwd':'aA123456'}
req=requests.post(url,data)
# print(req.json())
print(req.text) #string,json串
# 3、入参是json类型的
import random
phone=random.randint(10000000000,99999999999)
url='http://api.xxx.cn/api/user/xxx_stu'
data={
"name": "小青kk",
"grade": "天蝎座",
"phone": phone,
"sex": "男",
"age": 28,
"addr": "河南省济源市北海大道32号"
}
req=requests.post(url,json=data)
print(req.json())
# 4、添加COOKIE
import random
url='http://api.xxx.cn/api/user/gold_add'
data={ "stu_id":468,"gold":"999"}
cookie={'xxxx':'337ca4cc825302b3a8791ac7f9dc4bc6'}
req=requests.post(url,data,cookies=cookie)
print(req.text)
# 5、添加header
import random
url='http://api.xxx.cn/api/user/all_stu'
header={ "Referer":'http://api.nnzhp.cn/'}
req=requests.get(url,headers=header)
print(req.json())
# 6、上传文件
import random
url='http://api.xxxx.cn/api/file/file_upload'
data={ "file":open('test1',encoding='utf-8')}
req=requests.post(url,files=data)
print(req.json())
import random
url='http://api.xxxx.cn/api/file/file_upload'
data={ "file":open(r'C:Usersxxxxxxxxx.png','rb')}
req=requests.post(url,files=data)
print(req.json())
7、下载文件
import random
url='http://www.xxx.cn/wp-content/uxxx/2018/01/soup.jpg'
req=requests.get(url)
print(req.content)#返回的二进制的
fw=open('s.jpg','wb')
fw.write(req.content)