egbertbouman / youtube-comment-downloader

Simple script for downloading Youtube comments without using the Youtube API
MIT License
857 stars 219 forks source link

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

Closed Kingki19 closed 1 week ago

Kingki19 commented 2 months ago

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)
  2. Save in CSV file

    comments_df.to_csv('comments_data.csv', index=False)
github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 1 week ago

This issue was closed because it has been stalled for 7 days with no activity.