youtube-comment-downloader icon indicating copy to clipboard operation
youtube-comment-downloader copied to clipboard

Tutorial How to Use `youtube-comment-downloader`, transform it in DataFrame, and download it in Excel/CSV

Open Kingki19 opened this issue 9 months ago • 0 comments

I made this to help myself in future if in case i need to download again comment from Youtube video. Also i help you guys because there's no tutorial about this in documentation. I hope you like it.

Here, use this code and copy it in Google Colab, or Python IDE, or Kaggle Notebook, or Jupyter Notebook.

# Initiate Library
import pandas as pd
from youtube_comment_downloader import *

# Initiate Downloader and Youtube_url
downloader = YoutubeCommentDownloader()
Youtube_URL = #Input Youtube video URL that you want to extract it's comment
comments = downloader.get_comments_from_url(Youtube_URL, sort_by=SORT_BY_POPULAR)

# Initiate a dictionary to save all comments from Youtube Video
all_comments_dict = {
    'cid': [],
    'text': [],
    'time': [],
    'author': [],
    'channel': [],
    'votes': [],
    'replies': [],
    'photo': [],
    'heart': [],
    'reply': [],
    'time_parsed': []
}

# Take all comment and save it in dictionary using for loop
for comment in comments:
    for key in all_comments_dict.keys():
        all_comments_dict[key].append(comment[key])

# Convert Dictionary to Dataframe using Pandas
comments_df = pd.DataFrame(all_comments_dict)

# Display Dataframe
display(comments_df)

If you want to save it add this:

  1. Save in excel file
comments_df.to_excel('comments_data.xlsx', index=False)
  1. Save in CSV file
comments_df.to_csv('comments_data.csv', index=False)

Kingki19 avatar May 06 '24 11:05 Kingki19