我在学习Python,我想开始使用Twitter的流媒体API。我知道如何构建一个非常基本的应用程序,该应用程序使用urllib2
来读取页面的内容,并在过去成功地完成了此操作。但是,此特定的Twitter流媒体API所需的安全连接https://
似乎不适用于urllib2。如何从使用Python的https链接获取JSON?
我试图从以下链接获取JSON:https://stream.twitter.com/1.1/statuses/filter.json?locations=-122.75,36.8,-121.75,37.8
通过写入以下内容:
import urllib2
url = "https://stream.twitter.com/1.1/statuses/filter.json?locations=-122.75,36.8,-121.75,37.8"
print urllib2.urlopen(url).read()
回溯,虽然说我没有被授权;我推测这是因为HTTPS SSL连接,研究支持这一点。所以我想我的问题是从需要这种SSL连接的链接获取JSON的最佳方式是什么?我也试过卷曲,但只能再次出现错误401。
下面是完整的回溯。
Traceback (most recent call last): File "", line 1, in File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default urllib2.HTTPError: HTTP Error 401: Unauthorized
非常感谢提前。
的API可能需要OAuth凭证或其他API密钥。 – Amber
啊。它确实表示它需要一个Twitter用户才能登录。我想这就是为什么当我在浏览器中输入url时(因为我已登录到Twitter),这个流的作用。它不需要OAuth,所以我可以将我的登录凭证添加到我的Python文件的头文件中? – zch