やりたいこと youtube api で
channels の 'statistics''subscriberCount'
と
search の 'snippet'、'channelTitle'
を pageToken を使って全部取得したいです
channelsには id='UC0FFHRFAAAAAAA' のようなチャンネルidが含まれておらず、
searchを使って idを取得して、channels の 'statistics''subscriberCount'
を取得したいと考えていました
for文で何個か取得できましたが、うまくforが回ってくれません
コードの2回目のfor文に > 引用テキストpageToken=pageToken が無いからうまくいかないのでしょうか?
#この部分↓
search_response = youtube.channels().list(part='statistics',id=item_1['snippet']['channelId'],maxResults=50).execute()
最後の部分も色々変えてみましたが、うまくいきませんでした
pageToken = response.get("nextPageToken")
詳しい方よろしくお願いします
from googleapiclient.discovery import build API_KEY = キーを入力しています YOUTUBE_API_SERVICE_NAME = 'youtube' YOUTUBE_API_VERSION = 'v3' youtube = build(YOUTUBE_API_SERVICE_NAME,YOUTUBE_API_VERSION,developerKey=API_KEY) pageToken = None while True: response = youtube.search().list(q='キーワードを入力しています',part='snippet',type='channel',maxResults=50, pageToken=pageToken).execute() for item_1 in response['items']: #この部分に search_response = youtube.channels().list(part='statistics',id=item_1['snippet']['channelId'],maxResults=50).execute() for item_2 in search_response['items']: #↓エラーが出ている部分です print(item_1['snippet']['channelTitle'],item_2['statistics']['subscriberCount']) #この部分も触ってみましたがうまくいきませんでした pageToken = response.get("nextPageToken") if pageToken is None: break
エラー内容です
〜〜この部分に14個取得できています〜〜
Traceback (most recent call last):
File "/Users/〜〜〜.py", line 23, in <module>
print(item_1['snippet']['channelTitle'],item_2['statistics']['subscriberCount'])
KeyError: 'subscriberCount'
回答1件
あなたの回答
tips
プレビュー