访问华盛顿特区WMATA API Python

2024-04-23 07:55:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用WMATA的website中的总线事件API,并通过Python访问它,但遇到了问题

我正在使用Pyce,我相信代码的Python3.2选项是我应该使用的

我刚刚得到响应“进程已完成,退出代码为0”

import http.client, urllib.request, urllib.parse, urllib.error, base64

headers = {
    # Request headers
    'api_key': '**My Subscription Key Here**',
}

params = urllib.parse.urlencode({
    # Request parameters
    'Route': '90',
})

try:
    conn = http.client.HTTPSConnection('api.wmata.com')
    conn.request("GET", "/Incidents.svc/json/BusIncidents?%s" % params, "{body}", headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

####################################