Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python可以';t解码网站UnicodeCodeer错误_Python_Decode_Urllib - Fatal编程技术网

Python可以';t解码网站UnicodeCodeer错误

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文件

我在python中使用decode时遇到问题,我正在尝试获取IMDB网站(示例地址:):

我得到一个错误:

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