Python+Requests module gets response content

The Requests module gets the response content

The response includes the response line, response header, and response body content, and the returned response information can be obtained through the Requests module. The obtained response content is also the actual result obtained by the execution of the interface test.

get response line

get response header

Get other response information

Code example:

# Import requests module 
import requests 

r = requests.get("https://www.baidu.com") 
print(r.status_code) #Response status code 
print(r.reason) #Response information 
print(r.headers) # Get the response header to return the dict type, you can continue to use get to get the specified value 
# Get other response information: 
print(r.url) #Get the request address 
print(r.cookies) #Get cookies 
print(r.encoding) #Get The encoding format of the response

response body

According to the situation of different response body content, the Requests module has four different processing methods

1. Ordinary text data

Requests automatically decodes content from the server. Most unicode character sets can be decoded seamlessly. After a request is made, Requests makes an educated guess about the encoding of the response based on the HTTP headers. Requests will use its guessed text encoding when you access response.text. response.text has a high probability of garbled characters after use, you can first use the response.encoding attribute to change the encoding, as shown below:

Code example:

# 1, normal text data 
url = "http://www.hnxmxit.com/" 
response = requests.get(url=url) 
print(response.content.decode("utf-8"))

View execution results:

 2. Binary data

For non-text requests, response.content can access the request response body in bytes. And the Requests module will automatically decode the response data of gzip and deflate transfer encoding for you

# 2, binary data 
response =requests.get("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2073439784,2147418910&fm=26&gp=0.jpg") 
print(type(response.content)) # output Binary image data type 
with open("E:/baidu.gif","wb") as f: # Write binary data to local file 
    f.write(response.content)

View execution results:

 Three, json data

There is a built-in JSON decoder response.json() in Requests to process the returned JSON data. After use, the returned data will be treated as a json data object in python. response.json() will throw an exception if JSON decoding fails.

url = 'https://api.weixin.qq.com/cgi-bin/token' 
data = {'Grant_type': 'Client_credential', 
        'Appid': 'WXF14419077856' ', 
        ' Sec RET ': 92A113BD4B5FFDC72144740dc7123C99'} 
Response = requests.get(url=url,params=data) 
# The response is str type, so we need to convert the response into json 
json_obj = response.json() 
token = json_obj['access_token'] 
print(token)

4. Original response content (generally not used)

If you need to get the raw socket response from the server, you can use response.raw. If you are sure to get it, you also need to set stream=True in the initial request.

# 4, Raw response content (generally not used) 
url = "https://www.baidu.com" 
response = requests.get(url=url,stream=True) 
print(response.raw.read(10))

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/Liuyanan990830/article/details/130116103