到目前为止我的代码:
import requests
import json
url = "https://stream.twitter.com/1/statuses/sample.json"
r = requests.get(url, auth = ('username', 'password'))
print r.status_code
当我在控制台中运行这个脚本时,它一直挂起,但是当我运行等效的 urllib2 时,我得到了很好的响应:
from urllib2 import *
password_mgr = HTTPPasswordMgrWithDefaultRealm()
url = "https://stream.twitter.com/1/statuses/sample.json"
password_mgr.add_password(None, url, 'username', 'password')
h = HTTPBasicAuthHandler(password_mgr)
opener = build_opener(h)
page = opener.open(url)
print page.getcode()
这将返回 200
。有人知道问题出在哪里吗?
编辑:另外,当我在上面的代码中调整密码时,我得到了相应的 401
响应。我认为这是因为发生了一些阻塞?
最佳答案
我也有这个问题,它只影响请求版本 > 0.13.5。 要修复,请在 requests.get 调用中禁用预取:
r = requests.get(url, auth = ('username', 'password'), prefetch=False)
关于python - requests.get 一直卡在 Twitter Api(Python 请求库)上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11903231/