Requests安裝
使用pip安裝命令:
pip install requests
打開cmd,輸入python然后導入requests如果安裝成功沒有任何提示
如果提示如下則說明安裝失敗
ImportError: No module named 'requests'
Requests 基礎應用
發送不同類型HTTP請求
requests庫內置了不同的方法來發送不同類型的http請求,用法如下所示:
import requests base_url = "http://httpbin.org" # 發生GET類型請求 r_get = requests.get(base_url + "/get") print(r_get.status_code) # 發生POST類型請求 r_post = requests.post(base_url + "/post") print(r_post.status_code) # 發生PUT類型請求 r_put = requests.put(base_url + "/put") print(r_put.status_code) # 發生DELETE類型請求 r_delete = requests.delete(base_url + "/delete") print(r_delete.status_code)
執行結果,200是狀態碼表示發送請求成功
200 200 200 200
參數傳遞
傳遞URL參數
一般在GET請i去中使用字符串(query string)來進行參數傳遞,在requests庫中使用方法如下:
param_data = {'hero': 'leesin'} r_data = requests.get(base_url + '/get', params=param_data) print(r_data.url) print(r_data.status_code)
執行結果
http://httpbin.org/get?hero=leesin
200
傳遞body參數
在post請求中,一般參數都在請求體(Request body)中傳遞,在Requests中用法如下:
form_data = {'hero': 'leesin'} r_body = requests.post(base_url + '/post', data=form_data) print(r_body.text)
執行結果
{ "args": {}, "data": "", "files": {}, "form": { "hero": "leesin" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.22.0" }, "json": null, "origin": "61.144.173.21, 61.144.173.21", "url": "https://httpbin.org/post" }
請求頭定制
如果你想為請求添加HTTP頭部,只要簡單傳遞一個dict給headers參數就可以了。
用法如下:
form_data = {'hero': 'leesin'} header = {'user-agent': 'Mozilla/5.0'} r_headers = requests.post(base_url + '/post', data=form_data, headers=header) print(r_headers.text)
返回值
{ "args": {}, "data": "", "files": {}, "form": { "hero": "leesin" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0" }, "json": null, "origin": "61.144.173.21, 61.144.173.21", "url": "https://httpbin.org/post" }
響應內容
當請求發送成功之后,我們可以獲取響應內容。如響應狀態碼,響應頭信息,響應體內容
form_data = {'hero': 'leesin'} header = {'user-agent': 'Mozilla/5.0'} r_response = requests.post(base_url + '/post', data=form_data, headers=header) # 獲取響應狀態碼 print(r_response.status_code) # 獲取響應頭信息 print(r_response.headers) # 獲取響應內容 print(r_response.text) # 將響應內容以json格式返回 print(r_response.json())
返回值
200 {'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Origin': '*', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json', 'Date': 'Fri, 28 Jun 2019 14:38:09 GMT', 'Referrer-Policy': 'no-referrer-when-downgrade', 'Server': 'nginx', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '258', 'Connection': 'keep-alive'} { "args": {}, "data": "", "files": {}, "form": { "hero": "leesin" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0" }, "json": null, "origin": "61.144.173.21, 61.144.173.21", "url": "https://httpbin.org/post" } {'args': {}, 'data': '', 'files': {}, 'form': {'hero': 'leesin'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '11', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'Mozilla/5.0'}, 'json': None, 'origin': '61.144.173.21, 61.144.173.21', 'url': 'https://httpbin.org/post'}