📜  处理HTTP Requests的响应

📅  最后修改于: 2020-10-21 08:31:02             🧑  作者: Mango


 

在本章中,我们将详细介绍从Requests模块收到的响应。我们将讨论以下细节-

  • 得到回应
  • JSON回应
  • RAW响应
  • 二进制响应

得到回应

我们将使用request.get()方法向URL发出请求。

import requests
getdata = requests.get('https://jsonplaceholder.typicode.com/users');    

getdata具有响应对象。它具有响应的所有详细信息。我们可以得到使用(文本)和(内容)的2种方式的响应。使用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 。使用请求模块,您可以获取所使用的编码,还可以根据需要更改编码。例如,要获取编码,可以使用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'}}]

RAW响应

如果您需要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

要从原始数据中读取更多内容,您可以按照以下步骤操作:

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 。二进制响应主要用于非文本请求。