This series of tutorials is for personal study notes. If you want to browse, you may need other programming language basics (such as C language), why? Because I am bad at writing, only I can understand it! !
- httpbin
The website httpbin can test various information of HTTP requests and responses,
such as cookies, ip, headers and login verification, etc., and supports GET, POST and other methods, which
is very helpful for web development and testing. It is written in Python + Flask and is an open source project.
Official website: http://httpbin.org/Open
source address: https://github.com/Runscope/httpbin - get request
1. Simple get request data1 import requests 2 get_url = ' http://www.httpbin.org/get ' 3 r = requests.get(get_url) #send get request 4 print r 5 print help(r) 6 print r.text #received content in bytes 7 print r.content #Received content in unicode
2. Use python to grab the Baidu homepage
1 import requests 2 r = requests.get( ' http://www.baidu.com ' ) 3 print r.content 4 #Open a file and store the web page information 5 with open( ' baidu.html ' , ' w ' ) as fil: 6 fil.write(r.content)
Three, get parameters
1 #You can first view the documentation of the get method through the help function 2 import requests 3 # print help(requests.get) 4 get_url = ' http://www.httpbin.org/get ' 5 myparams={ ' qq ' : ' 34782655 ' } 6 # r = requests.get(url=get_url,params=myparams) 7 r = requests.get(get_url,myparams) 8 print r.url #Request url , concatenate url and parameters 9 print r. content #returned data
- post request 1. Example
of sending request body :
1 import requests 2 # Let's check the documentation of the post method first 3 # print help(requests.post) 4 #Pass the parameters through data, which is equivalent to the passed form, you can see in the returned data 5 post_url = ' http://www.httpbin.org/post ' 6 myData={ ' qq ' : ' 34782655 ' , ' cnblog ' : ' hyyq ' } 7 r = requests.post(url = post_url,data = myData) 8 print r .url #The requested url, the request body is parameter 9 printr.content #returned data
output:
1 http://www.httpbin.org/post 2 { 3 "args": {}, 4 "data": "", 5 "files": {}, 6 "form": { 7 "cnblog": "hyyq", 8 "qq": "34782655" 9 }, 10 "headers": { 11 "Accept": "*/*", 12 "Accept-Encoding": "gzip, deflate", 13 "Connection": "close", 14 "Content-Length": "23", 15 "Content-Type": "application/x-www-form-urlencoded", 16 "Host": "www.httpbin.org", 17 "User-Agent": "python-requests/2.18.4" 18 }, 19 "json": null, 20 "origin": "183.230.42.10", 21 "url": "http://www.httpbin.org/post" 22 } 23 24 [Finished in 1.3s]
2. Example of sending json data :1 import requests,json 2 post_url = ' http://www.httpbin.org/post ' 3 myData={ ' qq ' : ' 34782655 ' , ' cnblog ' : ' hyyq ' } 4 r = requests.post(url = post_url,json = myData) 5 # r = requests.post(url = post_url,data = json.dumps(myData)) 6 print r.url #Requested url , send json data 7 print r.content #Returned data
output:
1 http://www.httpbin.org/post 2 { 3 "args": {}, 4 "data": "{\"qq\": \"34782655\", \"cnblog\": \"hyyq\"}", 5 "files": {}, 6 "form": {}, 7 "headers": { 8 "Accept": "*/*", 9 "Accept-Encoding": "gzip, deflate", 10 "Connection": "close", 11 "Content-Length": "36", 12 "Content-Type": "application/json", 13 "Host": "www.httpbin.org", 14 "User-Agent": "python-requests/2.18.4" 15 }, 16 "json": { 17 "cnblog": "hyyq", 18 "qq": "34782655" 19 }, 20 "origin": "183.230.42.10", 21 "url": "http://www.httpbin.org/post" 22 } 23 24 [Finished in 1.0s]
- Upload file
example:1 #Upload files 2 import requests 3 # print help(requests) 4 post_url = ' http://www.httpbin.org/post ' 5 files = { ' file ' :open( ' yyc.txt ' , ' rb ' ) } #Here rb is to open in binary format for reading 6 r = requests.post(post_url,files= files) 7 print r.content
output:
1 { 2 "args": {}, 3 "data": "", 4 "files": { 5 "file": "data:application/octet-stream;base64,xOO6ww==" 6 }, 7 "form": {}, 8 "headers": { 9 "Accept": "*/*", 10 "Accept-Encoding": "gzip, deflate", 11 "Connection": "close", 12 "Content-Length": "147", 13 "Content-Type": "multipart/form-data; boundary=8bffc774646942deaa8a314f8675d879", 14 "Host": "www.httpbin.org", 15 "User-Agent": "python-requests/2.18.4" 16 }, 17 "json": null, 18 "origin": "183.230.42.10", 19 "url": "http://www.httpbin.org/post" 20 } 21 22 [Finished in 1.0s]