The difference between response.text and response.content in python

requests.content returns the binary response content

And requests.text guesses the encoding based on the response of the web page. If the server does not specify it, the default encoding is ""

ISO-8859-1" (When I first saw this, I was thinking why the default encoding is not set to utf8, and then I saw that the http protocol is like this, so...) So this is why you use response .text returns the reason for garbled characters.

You can use response.encoding to see what the encoding he guessed is. I guess it should be ISO-8859-1

Then you use response.encoding = 'utf-8'

Then response.text, the returned content should be normal.

>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1'

See the screenshot below, I have declared that the encoding is utf8, then the response.text will be decoded with utf8


Simply put:

  • resp.text returns Unicode data.
  • What resp.content returns is bytes, that is, binary data.

Guess you like

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