本系列教程供我的學習筆記使用,若是您要瀏覽可能須要其它編程語言基礎(如C語言),why?由於我寫得爛啊,只有我本身看得懂!!html
httpbin這個網站能測試 HTTP 請求和響應的各類信息,
好比 cookie、ip、headers 和登陸驗證等,且支持 GET、POST 等多種方法,
對 web 開發和測試頗有幫助。它用 Python + Flask 編寫,是一個開源項目。
官方網站:http://httpbin.org/
開源地址:https://github.com/Runscope/httpbinpython
1 import requests 2 get_url = 'http://www.httpbin.org/get' 3 r = requests.get(get_url) #發送get請求 4 print r 5 print help(r) 6 print r.text #接收的內容in bytes 7 print r.content #接收的內容in unicode
2、使用python抓去百度主頁git
1 import requests 2 r = requests.get('http://www.baidu.com') 3 print r.content 4 #打開一個文件,並存入網頁信息 5 with open('baidu.html','w') as fil: 6 fil.write(r.content)
3、get傳參github
1 #能夠先經過help函數來查看下get方法的文檔說明 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 #請求的url,拼接url和參數 9 print r.content #返回的數據
1 import requests 2 #先來查看下post方法的文檔使用說明 3 # print help(requests.post) 4 #經過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 #請求的url,請求體就是參數 9 print r.content #返回的數據
輸出:web
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、發送json數據
例子:編程
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 #請求的url,發送json數據 7 print r.content #返回的數據
輸出:json
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]
1 #上傳文件 2 import requests 3 # print help(requests) 4 post_url = 'http://www.httpbin.org/post' 5 files = {'file':open('yyc.txt','rb')} #這裏rb是以讀的方式用二進制格式打開 6 r = requests.post(post_url,files=files) 7 print r.content
輸出:cookie
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]