-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
10 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
from spider_brew_kit.contants import ENCODINGS | ||
|
||
|
||
def fix_encode(garbled_text: str, decoding='utf-8') -> (str, str): | ||
def fix_encode(garbled_text: str, decoding='utf-8', return_encoding=False): | ||
""" | ||
修复乱码 | ||
:param garbled_text: 乱码文本 | ||
:param decoding: 解码方式 | ||
:param return_encoding: 是否返回编码 | ||
:return: 正常文本, 编码 | ||
""" | ||
for encoding in ENCODINGS: | ||
try: | ||
res = garbled_text.encode(encoding).decode(decoding) | ||
return res, encoding | ||
if return_encoding: | ||
return res, encoding | ||
else: | ||
return res | ||
except (UnicodeEncodeError, UnicodeDecodeError): | ||
continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters