質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

2124閲覧

Python_Key errorを解決したい

shiratama_f

総合スコア2

Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/01/10 05:52

編集2022/01/10 05:59

前提・実現したいこと

Youtube APIを活用して特定チャンネルの動画情報取得から分析を行いたいと思っています。

初心者ですが、WEBサイトの情報を参考にコードを書いてみたものの、Key errorが発生し、先に進めなくなりました。teratail内でも同様の質問に対するコメントがあり、それを試したもののエラー解消に至りませんでした。どなたか解決方法をご教示いただけると幸いです。

ちなみにですが、下記の2つのWebサイトを参考にしています。
https://su-gi-rx.com/archives/4528#google_vignette
https://teratail.com/questions/332717

発生している問題・エラーメッセージ

エラーメッセージ Key error ### 該当のソースコード API_KEY = '伏せていますが自身のAPI Keyを入力しています' YOUTUBE_API_SERVICE_NAME = 'youtube' YOUTUBE_API_VERSION = 'v3' CHANNEL_ID = '伏せていますが該当のチャンネルIDを入力しています' channels = [] #チャンネル情報を格納する配列 searches = [] #videoidを格納する配列 videos = [] #各動画情報を格納する配列 nextPagetoken = None nextpagetoken = None youtube = build( YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=API_KEY ) channel_response = youtube.channels().list( part = 'snippet,statistics', id = CHANNEL_ID ).execute() for channel_result in channel_response.get("items", []): if channel_result["kind"] == "youtube#channel": channels.append([channel_result["snippet"]["title"],channel_result["statistics"]["subscriberCount"],channel_result["statistics"]["videoCount"],channel_result["snippet"]["publishedAt"]]) while True: if nextPagetoken != None: nextpagetoken = nextPagetoken search_response = youtube.search().list( part = "snippet", channelId = CHANNEL_ID, maxResults = 50, order = "date", #日付順にソート pageToken = nextpagetoken #再帰的に指定 ).execute() for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": searches.append(search_result["id"]["videoId"]) try: nextPagetoken = search_response["nextPageToken"] except: break for result in searches: video_response = youtube.videos().list( part = 'snippet,statistics', id = result ).execute() for video_result in video_response.get("items", []): if video_result["kind"] == "youtube#video": - videos.append([video_result["snippet"]["title"],video_result["statistics"]["viewCount"],video_result["statistics"]["likeCount"],video_result["statistics"]["dislikeCount"],video_result["statistics"]["commentCount"],video_result["snippet"]["publishedAt"]]) + videos.append([video_result["snippet"]["title"],video_result["statistics"]["viewCount"],video_result["statistics"].get("likeCount"),video_result["statistics"].get("dislikeCount"),video_result["statistics"].get("commentCount"),video_result["snippet"]["publishedAt"]]) videos_report = pd.DataFrame(videos, columns=['title', 'viewCount', 'likeCount', 'dislikeCount', 'commentCount', 'publishedAt']) videos_report.to_csv("videos_report.csv", index=None) channel_report = pd.DataFrame(channels, columns=['title', 'subscriberCount', 'videoCount', 'publishedAt']) channel_report.to_csv("channels_report.csv", index=None) ```Python --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-16-e5790d289859> in <module>() 53 for video_result in video_response.get("items", []): 54 if video_result["kind"] == "youtube#video": ---> 55 - videos.append([video_result["snippet"]["title"],video_result["statistics"]["viewCount"],video_result["statistics"]["likeCount"],video_result["statistics"]["dislikeCount"],video_result["statistics"]["commentCount"],video_result["snippet"]["publishedAt"]]) 56 + videos.append([video_result["snippet"]["title"],video_result["statistics"]["viewCount"],video_result["statistics"].get("likeCount"),video_result["statistics"].get("dislikeCount"),video_result["statistics"].get("commentCount"),video_result["snippet"]["publishedAt"]]) 57 KeyError: 'dislikeCount'

試したこと

teratailの質問に対する回答を参考にしましたが、解決できませんでした。上記にURLを記載しています。

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

Google Colaboratory
ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

下記のコードに修正したところ解決できました。
videos.append([video_result["snippet"]["title"],video_result["statistics"]
["viewCount"],video_result["statistics"]
["likeCount"],video_result["statistics"]
["dislikeCount"],video_result["statistics"]
["commentCount"],video_result["snippet"]["publishedAt"]])

投稿2022/01/10 06:37

shiratama_f

総合スコア2

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問