Text and content in python2 and python3---requests

At this age, we long for peace in our hearts, but we are not reconciled to mediocrity. Some people will never forget when they meet briefly.

The difference between python2 and python3 text-content

There are two ways to obtain the raw data obtained by requests, one is r.content and the other is r.text.
The following is python2
Insert picture description here
Insert picture description here
text is generally used to return text, which needs to be re-encoded, and then converted into a dictionary by json.loads()

result.text.encode('utf-8')

Content is generally used to return other data types

The following analysis of an encoding type of error:

msg is a Chinese string in front of the uniccode type, python default encoding type ascii, ascii cannot encode unicode type characters resulting in an error
Insert picture description here
Insert picture description here

The following is for python3:

Bytes are processed in bytes, and string str is processed in characters.
Encoding str—bytes encode
decoding bytes—str decode
text returns str
content returns bytes
result.content.decode('utf-8 ')
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37304462/article/details/114422370
Recommended