我试图在Python中使用Strava API v3,恐怕我遗漏了一些东西。医生说:
This base URL is used for all Strava API requests: https://api.strava.com
$ curl -i https://api.strava.com
HTTP/1.1 200 OK Content-Type: application/json Status: 200 OK X-RateLimit-Limit: 5000 X-RateLimit-Remaining: 4999 Content-Length: 2
Responses are in JSON format and gzipped.
我目前正在执行以下操作:
import urllib
print urllib.urlopen('https://api.strava.com').read()
得到这个:
^{pr2}$我不知道从哪里开始,因为我对HTTP请求和HTTPS知之甚少
更新:根据Merlin建议使用requests
模块,我正在这样做:
import requests
r = requests.get('https://api.strava.com/')
print r.status_code
print r.headers['content-type']
print r.encoding
print r.text
print r.json()
但是不断地得到一个错误:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.strava.com', port=443): Max retries exceeded with url: / (Caused by <class 'so cket.gaierror'>: [Errno 11004] getaddrinfo failed)
您需要先按照这里的说明操作:http://strava.github.io/api/v3/oauth/ 基本上,你创建的应用程序必须授权使用它的用户(在本例中是你)。我在下面写了一些示例代码。我是python新手,所以不知道如何自动登录,所以您必须将url复制并粘贴到浏览器,然后复制并粘贴代码。在
玩得开心!在
您需要使用httplib。访问HTTPS服务器的示例代码:
尝试使用请求!这样比较安全。 http://docs.python-requests.org/en/latest/
相关问题 更多 >
编程相关推荐