Python's network request, mainly makes the HTTP protocol class interface call, carries on the interface test and so on.
One, Urllib library, Python embedded library, not very useful.
fromUrllibImportRequest,parseImportJSON#url = ' http://baidu.com '#req = request.urlopen (URL) #打开一个url#content = Req.read (). Decode ()#FW = open (' nnzhp.html ', ' W ', encoding= ' utf-8 ')#fw.write (content)##网络爬虫, get some useful content from other websites#GET Request#url = ' http://www.baidu.com/'#req = request.urlopen (URL)#content = Req.read (). Decode () #获取返回结果#res_dic = json.loads (content) #返回的结果转为字典#if Res_dic.get (' error_code ') = = 0:#print (' Test pass. ')#Else:#print (' Test Failed ', Res_dic)#POST RequestURL ='Http://xxxxxx/api/user/login'Data= { 'Usernmae':'Admin', 'passwd':'aA123456'}#Request Datadata =parse.urlencode (data)#Print (Parse.urlencode (data)) #urlencode, automatically spell the parameters for youreq = Request.urlopen (URL, Data.encode ())#send a POST requestPrint(Req.read (). Decode ())
Second, requests module, easy to use module
ImportRequestsImportRandom#1. Send GET request#url = ' Http://xxxxxx/api/user/stu_info '#data = {' Stu_name ': ' xxxx '} #请求数据#req = requests.get (URL, params=data)#print (Req.json ())#print (Req.text)#print (Type (Req.json ())) #dict#print (Type (req.text)) #str#What's the return?#What is the returned type?#How does the Chinese do?#2. Post RequestURL ='Http://xxxxxxx/api/user/login'Data= {'username':'xxxxx','passwd':'xxxxx'}#Request Datareq =requests.post (URL, data)Print(Req.json ())#3, the entry is the JSON type#url = ' Http://xxxxxx/api/user/add_stu '#phone = random.randint (10000000000,99999999999)#data = {#"name": "XXXX",#" Grade": "Scorpio",#"Phone":p Hone,#" Sex": "Female",#"Age":#" addr": "No. 32nd, Beihai Road, Jiyuan, Henan province"# }#req = requests.post (URL, json=data)#print (Req.json ())#4, the entry is the JSON type#url = ' Http://xxxxxxx/api/user/gold_add '#data = {' stu_id ': 468, ' Gold ': 120000}#req = requests.post (URL, data, cookies={' Niuhanyang ': ' 337ca4cc825302b3a8791ac7f9dc4bc6 '})#print (Req.json ())#5. Add Header#url = ' Http://xxxxxxx/api/user/all_stu '#Header = {' Referer ': ' http://api/xxxxxxx/'}#req = requests.get (URL, headers=header)#print (Req.json ())#{' Error_code ': 4, ' msg ': ' Request routed incorrectly '}#6. Uploading Files#url = ' http://xxxxxxxx/api/file/file_upload '## data = {' file ': Open (' Note-day08.txt ', encoding= ' Utf-8 ')} #上传文件#data = {' file ': Open (' 12222.jpg ', ' RB ')} #上传图片#req = requests.post (URL, files=data)#print (Req.json ())#7. Download Pictures#http://xxxxx/wp-content/uploads/2018/01/soup.jpg#url = ' http://wwwxxxxx/wp-content/uploads/2018/01/soup.jpg '#req = requests.get (URL)#print (req.content) #返回的二进制的东西##FW = open (' s.jpg ', ' WB ')#fw.write (req.content)#url = ' http://wwwxxxxxx/'#req = requests.get (URL)#print (req.content) #返回的二进制的东西##FW = open (' pymysql.html ', ' WB ')#fw.write (req.content)#Http://up.mcyt.net/?down/46779.mp3URL ='Http://up.mcyt.net/?down/46779.mp3'req=requests.get (URL)Print(req.content)#returns the binary stuffFW = Open ('Aaaa,.mp3','WB') Fw.write (req.content)
Python learning note-day8-3-"Python network request and requests module"