# import urllib.request
# import json
import requests
# url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=ybq123'
# res = urllib.request.urlopen(url) # 發送請求
# res_new = res.read().decode() # 獲取結果
# print(res_new)
# 發送get請求
# res = requests.get(url) # 發送get請求
# print(res.text) # 獲取結果
# print(type(res.text))
# print(json.loads(res.text))
# print(res.json()) # 獲取結果直接就是字典,必須返回的是json串的話,才能用json()方法
# 發送post請求
# url = 'http://api.nnzhp.cn/api/user/login'
# data = {'username': 'niuhanyang', 'passwd': 'aA123456'}
# req = requests.post(url, data) # 發送post請求,第一個參數是url,第二個參數是請求的數據
# print(req.json())
# print(req.text)
#
# url1 = 'http://api.nnzhp.cn/api/user/user_reg'
# data = {
# 'username': 'wangsilei',
# 'pwd': 'wang123A',
# 'cpwd': 'wang123A'
# }
# res = requests.post(url1, data)
# print(res.text)
# 入參是json的
# url = 'http://api.nnzhp.cn/api/user/add_stu'
# data = {
# 'name': '王思磊',
# 'grade': '射手座',
# 'phone': 18033555656
# }
# req = requests.post(url, json=data)
# print(req.json())
# 添加cookie的
# 8b7d1b49fc8be17afe68bc67c82ea98b
# url = 'http://api.nnzhp.cn/api/user/gold_add'
# data = {
# 'stu_id': 231,
# 'gold': 10000
# }
# cookie = {'niuhanyang': '8b7d1b49fc8be17afe68bc67c82ea98b'}
# req = requests.post(url, data, cookies=cookie)
# print(req.json())
# 添加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())
# 上傳文件
# url = 'http://api.nnzhp.cn/api/file/file_upload'
# with open(r'C:\Users\wangsilei\Music\鳳凰傳奇\鳳凰傳奇 - 愛的狂怒.mp3', 'rb') as f:
# r = requests.post(url, files={'file': f})
# print(r.json())
# 下載文件
# url = 'http://img.lelezone.com/thumb/p/imgs/20/20390/20390_10.jpg'
# r = requests.get(url)
# print(r.status_code) # 獲取請求的狀態碼
# print(r.content) # 獲取返回結果二進制格式的
# with open('dog.jpg', 'wb') as f:
# f.write(r.content)
# 保存網頁
# url = 'http://www.nnzhp.cn/archives/630'
# res = requests.get(url)
# with open('nnzhp_630.html', 'wb') as f:
# f.write(res.content)