
- Requests 教程
- Requests - 首頁
- Requests - 概述
- Requests - 環境設定
- Requests - Http 請求是如何工作的?
- Requests - 使用 Requests
- 處理 HTTP 請求的響應
- Requests - HTTP 請求頭
- Requests - 處理 GET 請求
- 處理 POST、PUT、PATCH 和 DELETE 請求
- Requests - 檔案上傳
- Requests - 使用 Cookie
- Requests - 處理錯誤
- Requests - 處理超時
- Requests - 處理重定向
- Requests - 處理歷史記錄
- Requests - 處理會話
- Requests - SSL 證書
- Requests - 身份驗證
- Requests - 事件鉤子
- Requests - 代理
- Requests - 使用 Requests 進行網頁抓取
- Requests 有用資源
- Requests - 快速指南
- Requests - 有用資源
- Requests - 討論
處理 HTTP 請求的響應
在本章中,我們將深入瞭解從 requests 模組接收到的響應的更多細節。我們將討論以下內容:
- 獲取響應
- JSON 響應
- 原始響應
- 二進位制響應
獲取響應
我們將使用 request.get() 方法向 URL 發出請求。
import requests getdata = requests.get('https://jsonplaceholder.typicode.com/users');
getdata 擁有響應物件。它包含響應的所有詳細資訊。我們可以透過兩種方式獲取響應,使用(text)和(.content)。使用 response.text 將以文字格式返回資料,如下所示:
示例
E:\prequests>python makeRequest.py [ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }, "phone": "1-770-736-8031 x56442", "website": "hildegard.org", "company": { "name": "Romaguera-Crona", "catchPhrase": "Multi-layered client-server neural-net", "bs": "harness real-time e-markets" } }, ]
您將看到響應與您在瀏覽器中檢視 URL 原始碼時顯示的內容相同,如下所示:

您也可以嘗試 .html URL 並使用 response.text 檢視內容,它將與瀏覽器中 .html URL 的原始碼內容相同。
現在,讓我們嘗試對同一 URL 使用 response.content 並檢視輸出。
示例
import requests getdata = requests.get('https://jsonplaceholder.typicode.com/users') print(getdata.content)
輸出
E:\prequests>python makeRequest.py b'[\n {\n "id": 1,\n "name": "Leanne Graham",\n "username": "Bret",\n "email": "Sincere@april.biz",\n "address": {\n "street": "Kulas Light ",\n "suite": "Apt. 556",\n "city": "Gwenborough",\n "zipcode": " 92998-3874",\n "geo": {\n "lat": "-37.3159",\n "lng": "81.149 6"\n }\n },\n "phone": "1-770-736-8031 x56442",\n "website": "hild egard.org",\n "company": {\n "name": "Romaguera-Crona",\n "catchPhr ase": "Multi-layered client-server neural-net",\n "bs": "harness real-time e-markets"\n }\n },\n {\n "id": 2,\n "name": "Ervin Howell",\n "username": "Antonette",\n "email": "Shanna@melissa.tv",\n "address": {\n "street": "Victor Plains",\n "suite": "Suite 879",\n "city": "Wisoky burgh",\n "zipcode": "90566-7771",\n "geo": {\n "lat": "-43.950 9",\n "lng": "-34.4618"\n }\n },\n "phone": "010-692-6593 x091 25",\n "website": "anastasia.net",\n "company": {\n "name": "Deckow-Crist", \n "catchPhrase": "Proactive didactic contingency",\n "bs": "synergize scalable supply-chains"\n }\n },\n {\n "id": 3,\n "name": "Clementine Bauch",\n "username": "Samantha",\n "email": "Nathan@yesenia.net", \n "address": {\n "street": "Douglas Extension",\n "suite": "Suite 847",\n "city": "McKenziehaven",\n "zipcode": "59590-4157",\n "ge o": {\n "lat": "-68.6102",\n "lng": "-47.0653"\n }\n },\n
響應以位元組形式給出。您將在響應的開頭看到字母b。使用 requests 模組,您可以獲取使用的編碼,並在需要時更改編碼。例如,要獲取編碼,您可以使用 response.encoding。
print(getdata.encoding)
輸出
utf-8
您可以按如下方式更改編碼:您可以使用您選擇的編碼。
getdata.encoding = 'ISO-8859-1'
JSON 響應
您還可以透過使用 response.json() 方法以 json 格式獲取 Http 請求的響應,如下所示:
示例
import requests getdata = requests.get('https://jsonplaceholder.typicode.com/users') print(getdata.json())
輸出
E:\prequests>python makeRequest.py [{'id': 1, 'name': 'Leanne Graham', 'username': 'Bret', 'email': 'Sincere@april. biz', 'address': {'street': 'Kulas Light', 'suite': 'Apt. 556', 'city': 'Gwenborough', 'zipcode': '92998-3874', 'geo': {'lat': '-37.3159', 'lng': '81.1496'}}, ' phone': '1-770-736-8031 x56442', 'website': 'hildegard.org', 'company': {'name': 'Romaguera-Crona', 'catchPhrase': 'Multi-layered client-server neural-net', 'bs': 'harness real-time e-markets'}}]
原始響應
如果您需要 Http URL 的原始響應,您可以使用 response.raw,並在 get 方法中新增stream = True,如下所示:
示例
import requests getdata = requests.get('https://jsonplaceholder.typicode.com/users', stream=True) print(getdata.raw)
輸出
E:\prequests>python makeRequest.py <urllib3.response.HTTPResponse object at 0x000000A8833D7B70>
要從原始資料中讀取更多內容,您可以按如下方式進行:
print(getdata.raw.read(50))
輸出
E:\prequests>python makeRequest.py b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x95\x98[o\xe38\x12\x85\xdf\xe7W\x10y\ xda\x01F\x82.\xd4m\x9f\xdc\x9dd\xba\xb7\x93\xf4\x06q\xef4\x06\x83A@K\x15\x89m'
二進位制響應
要獲取二進位制響應,我們可以使用 response.content。
示例
import requests getdata = requests.get('https://jsonplaceholder.typicode.com/users') print(getdata.content)
輸出
E:\prequests>python makeRequest.py b'[\n {\n "id": 1,\n "name": "Leanne Graham",\n "username": "Bret",\n "email": "Sincere@april.biz",\n "address": {\n "street": "Kulas Light ",\n "suite": "Apt. 556",\n "city": "Gwenborough",\n "zipcode": " 92998-3874",\n "geo": {\n "lat": "-37.3159",\n "lng": "81.149 6"\n }\n },\n "phone": "1-770-736-8031 x56442",\n "website": "hildegard.org",\n "company": {\n "name": "Romaguera-Crona",\n "catchPhr ase": "Multi-layered client-server neural-net",\n "bs": "harness real-time e-markets"\n }\n },\n {\n "id": 2,\n "name": "Ervin Howell",\n "us ername": "Antonette",\n "email": "Shanna@melissa.tv",\n "address": {\n "street": "Victor Plains",\n "suite": "Suite 879",\n "city": "Wisoky burgh",\n "zipcode": "90566-7771",\n "geo": {\n "lat": "-43.950 9",\n "lng": "-34.4618"\n }\n },\n "phone": "010-692-6593 x091 25",\n "website": "anastasia.net",\n "company": {\n "name": "Deckow-Crist", \n "catchPhrase": "Proactive didactic contingency",\n "bs": "syn ergize scalable supply-chains"\n }\n },\n {\n "id": 3,\n "name": "Clementine Bauch",\n "username": "Samantha",\n "email": "Nathan@yesenia.net", \n "address": {\n "street": "Douglas Extension",\n "suite": "Suite 847",\n "city": "McKenziehaven",\n "zipcode": "59590-4157",\n " geo": {\n "lat": "-68.6102",\n "lng": "-47.0653"\n }\n },\n
響應以位元組形式給出。您將在響應的開頭看到字母b。二進位制響應主要用於非文字請求。
廣告