如何访问 python 字典中的特定项目?

时间:2021-01-02 09:24:09

标签: python

我试图使用 google-api-python-client 并且我基本上尝试访问 youtube 频道的观看次数。

response = {'kind': 'youtube#channelListResponse', 'etag': 'ZTMivxEyhYPJqH2CZb0MvzTZ4ts', 'pageInfo': {'totalResults': 1, 'resultsPerPage': 5}, 'items': [{'kind': 'youtube#channel', 'etag': 'Yxjey4KVgrhWSNhMTq1m1_52CJ0', 'id': 'UCCezIgC97PvUuR4_gbFUs5g', 'statistics': {'viewCount': '49964323', 'subscriberCount': '692000', 'hiddenSubscriberCount': False, 'videoCount': '230'}}]}

如何访问 viewCount 值?

from googleapiclient.discovery import build

api_key = "(private)"

youtube = build('youtube', "v3", developerKey=api_key)

request = youtube.channels().list(
    part="statistics",
    forUsername="schafer5"
)
response = request.execute()
print(response)

1 个答案:

答案 0 :(得分:1)

这种特定情况中,您可以执行以下操作:

response["items"][0]["statistics"]["viewCount"]