我在使用python中的解码时遇到问题,我正在尝试获取一个IMDB网站(示例地址:http://www.imdb.com/title/tt2216240/):
req = urllib.request.Request(address)
response = urllib.request.urlopen(req)
page = response.read().decode('utf-8', 'ignore')
with open('film.html', 'w') as f:
print(page, file=f)
我收到错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\xe6' in position 4132: character maps to <undefined>
答案 0 :(得分:0)
尝试明确指定utf-8
文件编码:
with open('film.html', 'w', encoding='utf-8') as f:
print(page, file=f)
答案 1 :(得分:0)