-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutubeapi.py
52 lines (43 loc) · 1.7 KB
/
youtubeapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pandas
import openpyxl
from googleapiclient.discovery import build
api_key = ''
video_id = 'E1-63UJuz6Q'
# https://www.youtube.com/watch?v=E1-63UJuz6Q
comments = list()
api_obj = build('youtube', 'v3', developerKey=api_key)
response = api_obj.commentThreads().list(part='snippet,replies', videoId=video_id, maxResults=100).execute()
while True:
for item in response['items']:
comment = item['snippet']['topLevelComment']['snippet']
comments.append(
[comment['textDisplay'], comment['authorDisplayName'], comment['publishedAt'], comment['likeCount']])
if item['snippet']['totalReplyCount'] > 0:
for reply_item in item['replies']['comments']:
reply = reply_item['snippet']
comments.append(
[reply['textDisplay'], reply['authorDisplayName'], reply['publishedAt'], reply['likeCount']])
if 'nextPageToken' in response:
response = api_obj.commentThreads().list(part='snippet,replies', videoId=video_id,
pageToken=response['nextPageToken'], maxResults=100).execute()
else:
break
df = pandas.DataFrame(comments)
df.to_excel('results.xlsx', header=['comment', 'author', 'date', 'num_likes'], index=None )
# api_youtube = build('youtube','v3',developerKey=api_key)
# response = api_youtube.search().list(
# q = '삼육대학교',
# order = 'relevance',
# part = 'snipet',
# maxResults = 100
# ).execute()
#
# video_list = []
# for temp in response['items']:
# try:
# video_id = temp['id']['videoId']
# print(video_id)
# video_list.append(video_id)
# print(temp['snippet']['title'])
# except:
# pass