我正在尝试在 Python 中使用Strava API v3,但恐怕我遗漏了一些东西。文档说:
此基本 URL 用于所有 Strava API 请求: 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
响应采用 JSON 格式并经过 gzip 压缩。
我目前正在这样做:
import urllib
print urllib.urlopen('https://api.strava.com').read()
并开始这样做:
Traceback (most recent call last):
File "StravaAPIv3.py", line 3, in <module>
print urllib.urlopen('https://api.strava.com').read()
File "C:\Python27\lib\urllib.py", line 86, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 207, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 436, in open_https
h.endheaders(data)
File "C:\Python27\lib\httplib.py", line 954, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 814, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 776, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 1157, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno 11004] getaddrinfo failed
我不知道从哪里开始,因为我不太了解 HTTP 请求和 HTTPS
更新:根据梅林使用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)