The Python access site can be--urllib modules with standard modules (omitted here), and requests (installation-PIP install requests) modules, requests modules are encapsulated on a urllib basis, More powerful than Urllib module, easy to use
Import json,requests# send Get request URL = ' Http://api.nnzhp.cn/api/user/stu_info?stu_name= small black horse ' req = Requests.get (URL) # Send GET request print (Req.text) #获取结果, return string/html etc with Textprint (Req.json ()) #获取结果直接就是字典, equivalent to print (Json.loads (req.text)) # JSON string into a dictionary, note: The JSON string must be returned 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 a URL, the second parameter is the request parameter 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 request parameter print (Req.json ()) #添加cookieurl = ' http://api.nnzhp.cn/api/user/gold_add ' data = {' stu_id ': 231, ' Gold ': 1000}cookie = {' Niuhanyang ': ' 6d195100b95a43046d2e385835c6e2c2 '}req = Requests.post (Url,data,cookies=cookie) Print (Req.json ()) #添加headerurl = ' Http://api.nnzhp.cn/api/user/all_stu ' h = {' Referer ': ' http://api.nnzhp.cn/', ' User-agent ': ' Chore '} #可以添加多个header, with, separate res = Requests.get (url,headers=h) 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) #获取返回结果--in binary format 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 ()
Python Network programming----requests Module