Am trying to create a function that uses Youtube API to fetch the comments and responses.
Please find my code below . This shows value error and doesn't work when am trying to store the data in DataFrame. I could understand that the issue is with the Reply Part but am not sure what went wrong. Any guidance or suggestions to tell me where I have gone wrong?
Update : I also noticed not all comments are getting extracted.
Thanks in Advance.
My Code :
def vid_comments():
Username = []
Comment = []
Comment_Likes = []
Comment_Date = []
Reply_Count = []
Reply = []
Replied_By = []
video_response=resource.commentThreads().list(part='snippet,replies',videoId=Video_ID).execute()
while video_response:
for item in video_response['items']:
item_info = item["snippet"]
topLevelComment = item_info["topLevelComment"]
comment_info = topLevelComment["snippet"]
Username.append(comment_info["authorDisplayName"])
Comment.append(comment_info["textDisplay"])
Comment_Likes.append(comment_info["likeCount"])
Comment_Date.append(comment_info['publishedAt'])
comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
replycount = item['snippet']['totalReplyCount']
Reply_Count.append(replycount)
if replycount>0:
for reply in item['replies']['comments']:
reply_info = reply["snippet"]
Reply.append(reply_info['textDisplay'])
Replied_By.append(reply_info['authorDisplayName'])
if 'nextPageToken' in video_response:
p_token = video_response['nextPageToken']
video_response = resource.commentThreads().list(part = 'snippet,replies',videoId = Video_ID,pageToken = p_token).execute()
else:
break
vid_comments()
cmt_data = {'Comment': Comment,'Username':Username,'Comment Likes Count':Comment_Likes,'Comment Date':Comment_Date,'Reply Count':Reply_Count}
Comment_Section=pd.DataFrame(cmt_data)
reply_data = {'Reply':Reply,'Replied_By':Replied_By}
Reply_Section = pd.DataFrame(reply_data)
This issues is solved with few modifications in the code