Python可以';t解码网站UnicodeCodeer错误
我在python中使用decode时遇到问题,我正在尝试获取IMDB网站(示例地址:): 我得到一个错误:Python可以';t解码网站UnicodeCodeer错误,python,decode,urllib,Python,Decode,Urllib,我在python中使用decode时遇到问题,我正在尝试获取IMDB网站(示例地址:): 我得到一个错误: UnicodeEncodeError: 'charmap' codec can't encode character '\xe6' in position 4132: character maps to <undefined> UnicodeEncodeError:“charmap”编解码器无法对位置4132中的字符“\xe6”进行编码:字符映射到 尝试明确指定utf-8文件
UnicodeEncodeError: 'charmap' codec can't encode character '\xe6' in position 4132: character maps to <undefined>
UnicodeEncodeError:“charmap”编解码器无法对位置4132中的字符“\xe6”进行编码:字符映射到
尝试明确指定utf-8
文件编码:
with open('film.html', 'w', encoding='utf-8') as f:
print(page, file=f)
你已经用过图书馆了吗
无论如何,它使:
谢谢,成功了!顺便说一句,它必须编码为='utf-8',因为第三个预期参数是缓冲的。@user2837976请注意,这只适用于python3。在python2中,内置的
open
没有编码
参数。但是,在这种情况下可以使用io.open
(在python3中open
是io.open
的同义词)
with open('film.html', 'w', encoding='utf-8') as f:
print(page, file=f)
#samplerequest.py
import requests
address = "http://www.imdb.com/title/tt2216240/"
req = requests.get(address)
print req.text
print req.encoding