【发布时间】:2012-04-04 11:10:33
【问题描述】:
我已经安装了名为 requests 的 python 包。 (pip install -i http://pip.kennethreitz.com/simple 请求)
首先,我尝试使用普通用户($)但没有成功,所以我尝试了根用户(#)并成功了。
问题出在使用上。当我尝试像
这样的简单代码时$python
>>>import requests
>>>r = requests.get('http://www.google.com')
>>>r.content
我得到了一个令人难以置信的烦人结果(请参阅此pastebin link。)
但是使用root
$sudo python
>>>import requests
>>>r = requests.get('http://www.google.com')
>>>r.content
我得到了正确合理的结果:pastebin。
我该如何解决这个问题? urllib.urlencode 也会发生这种情况。 (可能是使用 sudo easy_install 安装的,但我不记得了)。
我在 /usr/local/lib/python2.6/dist-packages 和 /usr/local/lib/python2.6/site-packages 但还是有同样的问题。
我真的认为问题出在 python 包上,因为这些代码非常简单。
系统详情:
Python 2.6.5
Ubuntu LTS 10.04.4
请求包 0.7.5
【问题讨论】:
-
如果这两种情况都发生,那么这不是 Python 问题。
-
也许不是。但是,正如我所写,代码运行良好。但是只要使用root,有什么问题。
-
试试
unicode(r.content, "utf-8")或r.content.encode("utf-8")看看会发生什么。
标签: python request package pip easy-install