質問編集履歴

1

文章の変更

2022/01/10 05:59

投稿

shiratama_f
shiratama_f

スコア2

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,5 @@
1
+ ### 前提・実現したいこと
2
+
1
3
  Youtube APIを活用して特定チャンネルの動画情報取得から分析を行いたいと思っています。
2
4
 
3
5
 
@@ -11,3 +13,185 @@
11
13
  https://su-gi-rx.com/archives/4528#google_vignette
12
14
 
13
15
  https://teratail.com/questions/332717
16
+
17
+
18
+
19
+ ### 発生している問題・エラーメッセージ
20
+
21
+
22
+
23
+ ```
24
+
25
+ エラーメッセージ
26
+
27
+ Key error
28
+
29
+
30
+
31
+ ### 該当のソースコード
32
+
33
+ API_KEY = '伏せていますが自身のAPI Keyを入力しています'
34
+
35
+ YOUTUBE_API_SERVICE_NAME = 'youtube'
36
+
37
+ YOUTUBE_API_VERSION = 'v3'
38
+
39
+ CHANNEL_ID = '伏せていますが該当のチャンネルIDを入力しています'
40
+
41
+ channels = [] #チャンネル情報を格納する配列
42
+
43
+ searches = [] #videoidを格納する配列
44
+
45
+ videos = [] #各動画情報を格納する配列
46
+
47
+ nextPagetoken = None
48
+
49
+ nextpagetoken = None
50
+
51
+
52
+
53
+ youtube = build(
54
+
55
+ YOUTUBE_API_SERVICE_NAME,
56
+
57
+ YOUTUBE_API_VERSION,
58
+
59
+ developerKey=API_KEY
60
+
61
+ )
62
+
63
+
64
+
65
+ channel_response = youtube.channels().list(
66
+
67
+ part = 'snippet,statistics',
68
+
69
+ id = CHANNEL_ID
70
+
71
+ ).execute()
72
+
73
+
74
+
75
+ for channel_result in channel_response.get("items", []):
76
+
77
+ if channel_result["kind"] == "youtube#channel":
78
+
79
+ channels.append([channel_result["snippet"]["title"],channel_result["statistics"]["subscriberCount"],channel_result["statistics"]["videoCount"],channel_result["snippet"]["publishedAt"]])
80
+
81
+
82
+
83
+ while True:
84
+
85
+ if nextPagetoken != None:
86
+
87
+ nextpagetoken = nextPagetoken
88
+
89
+
90
+
91
+ search_response = youtube.search().list(
92
+
93
+ part = "snippet",
94
+
95
+ channelId = CHANNEL_ID,
96
+
97
+ maxResults = 50,
98
+
99
+ order = "date", #日付順にソート
100
+
101
+ pageToken = nextpagetoken #再帰的に指定
102
+
103
+ ).execute()
104
+
105
+
106
+
107
+ for search_result in search_response.get("items", []):
108
+
109
+ if search_result["id"]["kind"] == "youtube#video":
110
+
111
+ searches.append(search_result["id"]["videoId"])
112
+
113
+
114
+
115
+ try:
116
+
117
+ nextPagetoken = search_response["nextPageToken"]
118
+
119
+ except:
120
+
121
+ break
122
+
123
+
124
+
125
+ for result in searches:
126
+
127
+ video_response = youtube.videos().list(
128
+
129
+ part = 'snippet,statistics',
130
+
131
+ id = result
132
+
133
+ ).execute()
134
+
135
+
136
+
137
+ for video_result in video_response.get("items", []):
138
+
139
+ if video_result["kind"] == "youtube#video":
140
+
141
+ - 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"]])
142
+
143
+ + 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"]])
144
+
145
+
146
+
147
+ videos_report = pd.DataFrame(videos, columns=['title', 'viewCount', 'likeCount', 'dislikeCount', 'commentCount', 'publishedAt'])
148
+
149
+ videos_report.to_csv("videos_report.csv", index=None)
150
+
151
+
152
+
153
+ channel_report = pd.DataFrame(channels, columns=['title', 'subscriberCount', 'videoCount', 'publishedAt'])
154
+
155
+ channel_report.to_csv("channels_report.csv", index=None)
156
+
157
+ ```Python
158
+
159
+ ---------------------------------------------------------------------------
160
+
161
+ KeyError Traceback (most recent call last)
162
+
163
+ <ipython-input-16-e5790d289859> in <module>()
164
+
165
+ 53 for video_result in video_response.get("items", []):
166
+
167
+ 54 if video_result["kind"] == "youtube#video":
168
+
169
+ ---> 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"]])
170
+
171
+ 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"]])
172
+
173
+ 57
174
+
175
+
176
+
177
+ KeyError: 'dislikeCount'
178
+
179
+ ```
180
+
181
+
182
+
183
+ ### 試したこと
184
+
185
+ teratailの質問に対する回答を参考にしましたが、解決できませんでした。上記にURLを記載しています。
186
+
187
+
188
+
189
+ ここに問題に対して試したことを記載してください。
190
+
191
+
192
+
193
+ ### 補足情報(FW/ツールのバージョンなど)
194
+
195
+ Google Colaboratory
196
+
197
+ ここにより詳細な情報を記載してください。