(2019-03-30追記) Windows の文字コードは Python では cp932 - criticablog によると shift_jisx0213
ではなく cp932
を使うべきだそうです。以下の記事は2015-07-30に投稿したものそのままです。
あるウェブページを、Python 3で以下のように読み込もうとしました。
import bs4 import urllib.request def get_soup(url): rawtext = urllib.request.urlopen(url).read() soup = bs4.BeautifulSoup(rawtext.decode('shift_jis')) get_soup('http://xxxxx')
すると、decodeの部分で以下のような例外が発生しました。
UnicodeDecodeError: 'shift_jis' codec can't decode byte 0x87 in position 30236: illegal multibyte sequence
python - decoding shift-jis: "illeagal multibyte sequence" - Stack Overflowに従い、
soup = bs4.BeautifulSoup(rawtext.decode('shift_jisx0213'))
と書き換えたところ、例外が発生しなくなりました。