无法打印出请求的文字?

时间:2013-07-07 19:22:20

标签: python character-encoding get request codec

我正在尝试在Python中打印出以下请求(使用 requests 模块):

r = requests.get("http://www.youtube.com", 
                params={
                    "search_query": "test"
                }).text 

但是,在打印时,我收到以下错误:

  

UnicodeEncodeError:'charmap'编解码器无法对字符进行编码   位置32891-32894:字符映射到<undefined>

我确定这不是模块本身,因为基于一些谷歌搜索似乎没有其他事件发生,并且它正在处理我尝试的其他请求。似乎只有这个请求无法打印。

有人知道怎么打印吗?

编辑:现在我真的很困惑:this program worked once for me, and has not worked once since that one occurence

1 个答案:

答案 0 :(得分:6)

对于python 2.x,您需要在打印之前对字符串进行编码。

尝试:

print r.encode('utf-8')
相关问题