Import Urllib.request
Import json,requests
#发送get请求
# url = ' http://api.nnzhp.cn/api/user/stu_info?stu_name= small black Horse '
# req = requests.get (URL) #发送get请求
# Print (req.text) #获取结果
# Print (Req.json ()) #获取结果直接就是字典, you must return a JSON string in order to use the. Json method.
#发送post请求
# url = ' Http://api.nnzhp.cn/api/user/login '
# data = {' username ': ' Niuhanyang ', ' passwd ': ' aA123456 '}
# req = Requests.post (url,data) #发送post请求, the first parameter is the URL, the second parameter is the requested data
# Print (Req.json ())
#入参是json的
# url = ' Http://api.nnzhp.cn/api/user/add_stu '
# data = {' name ': ' Ding ', ' Grade ': ' Cancer ', ' phone ': 31971891223}
# req = Requests.post (url,json=data) #发送post请求, the first parameter is the URL, the second parameter is the requested data
# Print (Req.json ())
#添加cookie
# url = ' Http://api.nnzhp.cn/api/user/gold_add '
# data = {' stu_id ': 231, ' Gold ': +}
# cookie = {' Niuhanyang ': ' 6D195100B95A43046D2E385835C6E2C2 '}
# req = Requests.post (Url,data,cookies=cookie)
# Print (Req.json ())
#添加header
# url= ' Http://api.nnzhp.cn/api/user/all_stu '
# MPP = {' Referer ': ' http://api.nnzhp.cn/', ' user-agent ': ' Chore '}
# res = requests.get (url,headers=mpp)
# Print (Res.json ())
#上传文件
# url = ' http://api.nnzhp.cn/api/file/file_upload '
# f = open (R ' C:\Users\bjniuhanyang\Desktop\ad.cpm.schedulingInfo.v1.json ', ' RB ')
# r = requests.post (url,files={' file ': F})
# Print (R.json ())
#下载文件
# url= ' http://www.besttest.cn/data/upload/201710/f_36b1c59ecf3b8ff5b0acaf2ea42bafe0.jpg '
# r = Requests.get (URL)
# Print (r.status_code) #获取请求的状态码
# Print (r.content) #获取返回结果二进制格式的
# FW = open (R ' bt.jpg ', ' WB ')
# Fw.write (r.content)
# fw.close ()
#保存网页
url = ' http://www.nnzhp.cn/archives/630 '
r = requests.get (URL)
f = open (' nnzhp.html ', ' WB ')
f.write (r.content)
f.close ()
Day7-python Study Notes (15) Network programming