中国語のページをダウンロードしようとすると (メタ タグによると gb2312 のようです)。以下のコードを実行し、gEdit で gb2312 形式のファイルを開くと、中国語の文字があるはずの ê×××(ò) などの意味不明な記号が表示されます。
問題のページのソース コードは次のとおりです: https://gist.github.com/anonymous/27663069655db7fd7a19 - 実際のサイトは教育機関専用です。
私のコード:
r = requests.post("http://example.com", data=payload, cookies=cookies)
f = open('myfile.txt', 'w')
f.write(r.text.encode('gb2312',errors="ignore"))
f.close()
ページのヘッダー:
{'content-length': '6164', 'x-powered-by': 'ASP.NET', 'date': 'Mon, 11 Mar 2013 05:11:24 GMT', 'cache-control': ' private', 'content-type': 'text/html', 'server': 'Microsoft-IIS/6.0'}
エンコードではなくデコードしようとすると、Python で次のエラーが発生します。
f.write(r.text.decode('gb2312',errors="ignore"))
UnicodeEncodeError: 'ascii' コーデックは位置 2017-2018 の文字をエンコードできません: 序数が範囲外です (128)