修复解码问题

This commit is contained in:
Jing Ling
2020-08-26 10:45:06 +08:00
parent 173b9fc72c
commit ea9249a939
+12 -6
View File
@@ -719,11 +719,17 @@ def get_url_resp(url):
def decode_resp_text(resp): def decode_resp_text(resp):
content = resp.content
if not content:
return str('')
try: try:
text = resp.text(encoding='utf-8', errors='strict') # 先尝试用utf-8严格解码 # 先尝试用utf-8严格解码
except UnicodeError: content = str(content, encoding='utf-8', errors='strict')
except (LookupError, TypeError, UnicodeError):
try: try:
text = resp.text(encoding='gb18030', errors='strict') # 再尝试用gb18030严格解码 # 再尝试用gb18030严格解码
except UnicodeError: content = str(content, encoding='gb18030', errors='strict')
text = resp.text(encoding=None, errors='ignore') # 最后尝试自动解码 except (LookupError, TypeError, UnicodeError):
return text # 最后尝试自动解码
content = str(content, errors='replace')
return content