1 import requests 2 3 1.发送get请求 4 url = 'http:///api/user/stu_info' 5 data = { 'stu_name':'小黑'} #请求数据 6 req = requests.get(url,params=data) #发送get请求 7 print(req.json()) #字典 单引号' 8 print(req.text) #string 双引号" 9 10 2.发送post请求11 url = 'http:///api/user/login'12 data = {13 'username':'niuhanyang',14 'passwd':'aA123456'15 } #请求数据16 req = requests.post(url,data)#发送post请求17 print(req.json())18 19 3.入参是json类型20 import random21 phone = random.randint(10000000000,99999999999)22 url = 'http:///api/user/add_stu'23 data = {24 "name":"yyy",25 "grade":"天蝎座",26 "phone":phone,27 "sex":"男",28 "age":28,29 "addr":"河南省济源市北海大道32号"30 }31 req = requests.post(url,json=data)32 print(req.json())33 34 4.添加cookie35 url = 'http:///api/user/gold_add'36 data = { 'stu_id':468,'gold':100000}37 cookie = { 'niuhanyang':'337ca4cc825302b3a8791ac7f9dc4bc6'}38 req = requests.post(url,data,cookies=cookie)39 print(req.json())40 41 42 5.添加header43 url = 'http:///api/user/all_stu'44 header = {45 'Referer':'http://api.nnzhp.cn/'46 }47 req = requests.get(url,headers = header)48 print(req.json())49 50 6.上传文件51 url = 'http:///api/file/file_upload'52 data = {53 # 'file':open('baidu.html',encoding='utf-8') #图片与excel是rb54 'file':open('E:\神州-BDCSC测试相关文档\getSumByViewId.csv','rb')55 }56 req = requests.post(url,files = data,)57 print(req.json())58 59 60 7.下载图片61 # url = 'http:///wp-content/uploads/2018/01/soup.jpg'62 url = 'url = 'http:///wp-content/uploads/2018/01/soup.jpg''63 req = requests.get(url)64 print(req.content) #返回的二进制65 fw = open('m.mp3','wb')#二进制写模式wb66 fw.write(req.content)