egbertbouman / youtube-comment-downloader

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

Can I grab all the comments of several videos at once? #52

Closed poya0524 closed 4 years ago

poya0524 commented 4 years ago

Hello, First thanks for the code, it works! Since I am a beginner of Python and I would like to download comments of 80 videos. (I have already made the list of the ids.) I am wandering Is it possible to download all of them at once and add video id in the output which looks like: {"video id": -----, "cid":------, "text":-------, etc) I guess maybe this can be deal with for loop...?

I'll really appreciate if someone can help me. Thank you!

egbertbouman commented 4 years ago

You can just call the download_comments function from a separate script. For instance:

import io, json
from downloader import download_comments

with open('input.txt', 'r') as fp:
    youtube_ids = fp.readlines()

for youtube_id in youtube_ids:
    print('Downloading', youtube_id)
    with io.open('output.txt', 'w', encoding='utf8') as fp:
        for comment in download_comments(youtube_id):
            comment['video_id'] = youtube_id
            comment_json = json.dumps(comment, ensure_ascii=False)
            print(comment_json.decode('utf-8') if isinstance(comment_json, bytes) else comment_json, file=fp)
spiralofhope commented 4 years ago

I'm guessing this is much cleaner than the method I was using with the pure shell.. :)

poya0524 commented 4 years ago

It works!!! Thank you so much!!

egbertbouman commented 4 years ago

You're welcome! Since your question seems to be answered, I'll close this issue.