不能用Python打印请求响应

时间:2017-04-18 22:05:37

标签: python encoding

Stack Overfellas!我使用Python 2.7.13与请求库,我想打印发送到网站的请求的响应:

import requests
r = requests.get("url")
print r.text

print r.text引发了错误:'charmap' codec can't encode character u'\u2018' in position 16311: character maps to <undefined>

我已经搜索过此错误,我得到的建议是使用r.encode提前定义编码:

r.encode = "utf-8"
r.text.encode('utf-8', 'ignore')

但它没有帮助,r.text引发了同样的错误。

1 个答案:

答案 0 :(得分:2)

您是否将text设置为新编码的文字?

text = u'\u2018'
text = text.encode('utf-8', 'ignore')
print text