如何阅读 Python 中的 application/octet-stream

How to read application/octet-stream in Python

基于 ,我正在使用 Python 脚本来调用 API,详见下文 link:

https://developer.wmata.com/docs/services/gtfs/operations/5cdc51ea7a6be320cab064fe?

我使用下面的代码调用 api:

import requests

# define functions
def _prepare_url(path):
    return f'{API_URL}/{path.lstrip("/")}'


def pull_data(path, params=None, headers=None):
    url =_prepare_url(path)
    return requests.get(url, params=params, headers=headers)


# print results in cleaner format
def jprint(obj):
    # create a formatted string of the Python JSON object
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)


API_URL = 'https://api.wmata.com'

# authenticate with your api key
headers = {
    "api_key": "myKey",
}

response = pull_data('/gtfs/bus-gtfsrt-tripupdates.pb', headers=headers)
print(response.content)
print(response.headers)
print(response.url)

但它 returns 无意义的数据流以及以下内容 headers:

Request-Context: appId=cid-v1:2833aead-1a1f-4ffd-874e-ef3a5ceb1de8
Cache-Control: public, must-revalidate, max-age=5
Date: Thu, 11 Feb 2021 22:05:31 GMT
ETag: 0x8D8CED90CC8419C
Content-Length: 625753
Content-MD5: fspEFl7LJ8QbZPgf677WqQ==
Content-Type: application/octet-stream
Expires: Thu, 11 Feb 2021 22:05:37 GMT
Last-Modified: Thu, 11 Feb 2021 22:04:49 GMT

'''b'\n\r\n\x031.0\x10\x00\x18\xd9\xef\xa5\x81\x06\x12\xee\x02\n\n1932817010\x1a\xdf\x02\n\x1a\n\n1932817010\x1a\x0820210214*\x0233\x12\x13\x08\x02\x1a\x06\x10\x9c\xff\xa5\x81\x06"\x0513752...'''

有没有关于如何阅读此类回复的指南?

GTFS-rt 在称为“protobuf”的压缩编码表示中传输。您的 Python 脚本将需要使用 gtfs-realtime.proto 文件(其中包含 GTFS-rt 提要的预期内容的定义)以及 Google Protobuf Python 包为了解码响应。

这里是如何从文档中读取 Python 中的 GTFS-rt API 的示例:https://developers.google.com/transit/gtfs-realtime/examples/python-sample.