requests module - common properties and methods of response objects

After using Requests to send a request, you will get a Response response object. In addition to the commonly used textand contentproperties, the Response object provides many other commonly used properties and methods.

Common properties of the Response response object

The following table lists common properties of the Response response object:

Attributes describe
url response URL
status_code response status code
headers response header information
request.headers Respond to the request header of the corresponding request
request._cookies Response to the cookie corresponding to the request
cookies Response Cookie (after set-cookie)
ok Determine whether the response was successful
is_redirect Determine whether there is a redirection
content Response content (byte type)
text Response content (text type)
encoding encoding of the response
elapsed response time
history The redirection history of the response

Common methods of Response response objects

The following table lists common methods of the Response response object:

method describe
json() Convert JSON response content to Python object (dictionary or list)
close() Close the response connection and release resources
iter_content() Obtain the byte data of the response content block by block in the form of an iterator
iter_lines() Get the text data of the response content line by line in an iterator
raise_for_status() If the response status code is not 200, an exception will be thrown
is_permanent_redirect() Determine whether it is a permanent redirect (301 status code)
is_redirect() Determine whether it is a redirection (3xx status code)
is_client_error() Determine whether it is a client error (4xx status code)
is_server_error() Determine whether it is a server error (5xx status code)
iter_any() Obtain any data of the response content byte by byte as an iterator

Note that the above properties and methods are only part of the Response response object. Depending on your actual needs, you may use some or more of them.

example

import requests

# 目标url
url = 'https://www.baidu.com'

# 向目标url发送get请求
response = requests.get(url)

# 响应的状态码
status_code = response.status_code

# 响应的URL,有时候响应的URL和请求的URL并不一致
response_url = response.url

# 响应的内容(字节类型)
content = response.content

# 响应的内容(文本类型)
text = response.text

# 响应头信息
headers = response.headers

# 响应对象的请求头信息
request_headers = response.request.headers

# 响应的cookies(经过了set-cookie动作)
cookies = response.cookies

# 响应对象的请求携带的cookies
request_cookies = response.request._cookies

# 判断响应是否成功
is_success = response.ok

# 判断是否存在重定向
is_redirect = response.is_redirect

# 判断是否存在内容
has_content = response.content is not None

# 判断是否存在文本内容
has_text = response.text is not None

# 获取响应头中的特定信息
content_type = response.headers.get('content-type')

# 获取响应时间
response_time = response.elapsed.total_seconds()

# 获取响应的编码
encoding = response.encoding

# 获取响应的历史记录,即重定向路径
history = response.history

# 将JSON响应内容转换为Python对象(字典或列表)
json_data = response.json()

# 关闭响应连接,释放资源
response.close()

Guess you like

Origin blog.csdn.net/m0_67268191/article/details/131754316