- 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": "[email protected]", "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": "[email protected]",\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": "[email protected]",\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": "[email protected]", \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": "[email protected]",\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": "[email protected]",\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": "[email protected]", \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。二进制响应主要用于非文本请求。
广告