Skip to main content
 首页 » 编程设计

python之无法使用 Python 打印请求响应

2024年10月01日7zengkefu

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引发相同的错误。

请您参考如下方法:

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

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