法一:设置response的encoding
import requests
res = requests.get("https://www.baidu.com/")
res.encoding = res.apparent_encoding
print(res.text)
法二:设置response的content解码
import requests
res = requests.get("https://www.baidu.com/")
txt = res.content.decode('gbk')
法三:使用chardet
import requests
import chardet
res = requests.get("https://www.baidu.com/")
encoding = chardet.detect(res.content)['encoding']
print(res.content.decode(encoding))
法四:使用cchardet
import requests
import cchardet
res = requests.get("https://www.baidu.com/")
encoding = cchardet.detect(res.content)['encoding']
print(res.content.decode(encoding))
法五:encode + decode
import requests
import cchardet
res = requests.get("https://www.baidu.com/")
res_encoding = res.encoding
con_encoding = cchardet.detect(res.content)['encoding']
print(res.text.encode(res_encoding).decode(con_encoding))