The Python Oracle

What is the difference between 'content' and 'text'

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Dreaming in Puzzles

--

Chapters
00:00 Question
01:28 Accepted answer (Score 209)
01:49 Answer 2 (Score 14)
02:10 Thank you

--

Full question
https://stackoverflow.com/questions/1701...

Question links:
[Python Requests]: http://www.python-requests.org/
[fine documentation]: http://www.python-requests.org/en/latest.../

Accepted answer links:
[documentation]: https://requests.readthedocs.io/en/maste...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pythonrequests

#avk47



ACCEPTED ANSWER

Score 232


The requests.Response class documentation has more details:

r.text is the content of the response in Unicode, and r.content is the content of the response in bytes.




ANSWER 2

Score 15


It seems clear from the documentation is that r.content

You can also access the response body as bytes, for non-text requests:

 >>> r.content

If you read further down the page it addresses for example an image file




ANSWER 3

Score 2


To some extent, text is nothing, but a sugar-coated version of content.

import requests
response = requests.get("https://httpbin.org/get")
response.text == response.content.decode(encoding == response.encoding)
True

All internet content is received in the form of bytes. Choose response.text for textual responses and use response.content for binary files like images or PDF