The difference between the content and text methods of python requests

Reprinted from "https://blog.csdn.net/xie_0723/article/details/51361006

 

I have been thinking about the difference between the content and text attributes of requests, and there is no difference from the print results.

Look at the source code:

    @property
    def text(self): """Content of the response, in unicode. If Response.encoding is None, encoding will be guessed using ``chardet``. The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set ``r.encoding`` appropriately before accessing this property. """ #content的完整代码就不贴了。 @property def content(self): """Content of the response, in bytes.""" 

 

  • The conclusion is:

resp.text returns Unicode data.

What resp.content returns is bytes, that is, binary data.


That is, if you want to get text, you can pass r.text.

If you want to get pictures and files, you can pass r.content.

(resp.json() returns data in json format)

  • take a chestnut
# 例如下载并保存一张图片

import requests

jpg_url = 'http://img2.niutuku.com/1312/0804/0804-niutuku.com-27840.jpg'

content = requests.get(jpg_url).content

with open('demo.jpg', 'wb') as fp: fp.write(content)
---------------------------------------------------------------------------------------以下是正确的

1. Focus on understanding

response.textThe returned type isstr

response.contentThe returned type is bytes, the type can be converted to a type by a decode()methodbytesstr

It is recommended to use: response.content.decode()to get the corresponding html page

2. Expand your understanding

  • response.text
    decoding type: make an educated guess on the encoding of the response based on the HTTP header, and the inferred text encoding
    How to modify the encoding method:response.encoding = 'gbk'
  • response.content
    decoding type: not specified
    How to modify the encoding method:response.content.decode('utf8')
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813643&siteId=291194637