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 random 21 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.添加cookie 35 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.添加header 43 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是rb 54 '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')#二進制寫模式wb 66 fw.write(req.content)