Importurllib.requestImportjson,requests#urlib modules, not commonly usedURL ='http://api.nnzhp.cn/api/user/stu_info?stu_name= Small Black Horse'Res=urllib.request.urlopen (URL) Jieguo=res.read (). Decode ()#The return value is of type byte and requires transcodingPrint(Json.loads (Jieguo))#send a GET requestURL ='http://api.nnzhp.cn/api/user/stu_info?stu_name= Small Black Horse'req= Requests.get (URL)#send a GET requestPrint(Req.text)#Get ResultsPrint(Req.json ())#The result is a dictionary, which must return a JSON string in order to use the. Json method. #send a POST requestURL ='Http://api.nnzhp.cn/api/user/login'Data= {'username':'Niuhanyang','passwd':'aA123456'}req= Requests.post (Url,data)#sends a POST request, the first parameter is the URL, the second parameter is the requested dataPrint(Req.json ())#The import parameter is JSON.URL ='Http://api.nnzhp.cn/api/user/add_stu'Data= {'name':'Ding','Grade':'Cancer','Phone': 31971891223}#It's a dictionary.req = Requests.post (url,json=data)#sends a POST request, the first parameter is the URL, the second parameter is the requested dataPrint(Req.json ())#Add a cookieURL ='Http://api.nnzhp.cn/api/user/gold_add'Data= {'stu_id': 231,'Gold': 1000}cookie= {'Niuhanyang':'6D195100B95A43046D2E385835C6E2C2'}req= Requests.post (url,data,cookies=cookies)Print(Req.json ())#Add HeaderUrl='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 ())#Uploading FilesURL ='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 ())#Download FileUrl='http://www.besttest.cn/data/upload/201710/f_36b1c59ecf3b8ff5b0acaf2ea42bafe0.jpg'R=requests.get (URL)Print(R.status_code)#Get the status code of the requestPrint(r.content)#gets the returned result in binary format.FW = Open (r'bt.jpg','WB') Fw.write (r.content) fw.close ()#Save Web pageURL ='http://www.nnzhp.cn/archives/630'R=requests.get (URL) F= Open ('nnzhp.html','WB') F.write (r.content) f.close ()
Python Learning note 7-network programming