作者:
yshihyu (yshihyu)
2016-10-23 00:14:15https://gist.github.com/anonymous/1078f4e653de5f1f9c6e98166b750212
加上 r.encoding = r.apparent_encoding 編碼確實就正常
python test.py 正常不會有亂碼
但我想我內容導向ruten.html會出現錯誤
python test.py > ruten.html
UnicodeEncodeError: 'ascii' codec can't encode characters in position
207-210: ordinal not in range(128)
錯誤 google 一下是解釋系統編碼是 ascii 編碼範圍只有0~127
但我加上下面兩行就可以成功導向 ruten.html
reload(sys)
sys.setdefaultencoding("utf-8")
好奇為什麼導向才會出現這問題? 而我程式裡面 print r.text 卻能正常輸出內容在終端機上?
謝謝