- Published on
Pythonでrequest.getしたHTMLデータの文字コード対策
- Authors

- Name
- nisyuu (にしゅう)
- @nishilyuu
request.getしたサイトのレスポンスヘッダに文字コード情報がないと、ISO-8859-1と判定されてしまいます。もし、ヘッダに文字コード情報がない場合は、対策が必要です。
通常
response = requests.get("http://www.example.com/")
response.encoding = response.apparent_encoding
大量のデータがある場合
cChardetを使うことで高速に処理できるそうです。
import cchardet
response = requests.get("http://www.example.com/")
response.encoding = cchardet.detect(response.content)["encoding"]
参考
https://kanji.hatenablog.jp/entry/python-requests-beautifulsoup-encoding