Get YouTube Comments in Python

Use YouTube's API to get comments on videos. I will omit the YouTube API.

manner

getYouTubeComments.py


import requests
import json

URL = 'https://www.googleapis.com/youtube/v3/'
#Enter API KEY here
API_KEY = 'Enter API KEY'

def print_video_comment(video_id, next_page_token):
  params = {
    'key': API_KEY,
    'part': 'snippet',
    'videoId': video_id,
    'order': 'relevance',
    'textFormat': 'plaintext',
    'maxResults': 100,
  }
  if next_page_token is not None:
    params['pageToken'] = next_page_token
  response = requests.get(URL + 'commentThreads', params=params)
  resource = response.json()

  for comment_info in resource['items']:
    #comment
    text = comment_info['snippet']['topLevelComment']['snippet']['textDisplay']
    #Good number
    like_cnt = comment_info['snippet']['topLevelComment']['snippet']['likeCount']
    #Number of replies
    reply_cnt = comment_info['snippet']['totalReplyCount']

    print('{}\t{}\t{}'.format(text.replace('\n', ' '), like_cnt, reply_cnt))
  
  if 'nextPageToken' in resource:
    print_video_comment(video_id, resource["nextPageToken"])

#Enter your Video ID here
video_id = 'Enter Video ID'
print_video_comment(video_id, None)

Example of execution result

The official channel of the Japan Sumo Association "Iron Chef of Takasaki Master-Dewanoumi Stable Chanko-" will be as follows. .. I'm running on Google Colaboratory.

The output results are in the order of comments, good numbers, and replies.

スクリーンショット 2020-09-27 23.29.45.png

reference

I referred to the following article. Thank you very much.

Recommended Posts

Get YouTube Comments in Python
Play youtube in python
Get Youtube data in Python using Youtube Data API
Get date in Python
[Python] Get all comments using Youtube Data API
Get last month in python
Get Youtube data with python
Get Terminal size in Python
Explicitly get EOF in python
Get Evernote notes in Python
Get Japanese synonyms in Python
Get comments on youtube Live with [python] and [pytchat]!
Get Leap Motion data in Python.
Get data from Quandl in Python
Get the desktop path in Python
Get the script path in Python
Get, post communication memo in Python
Get the desktop path in Python
Get the host name in Python
Get started with Python in Blender
Get additional data in LDAP with python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Get exchange rates from open exchange rates in Python
Get Suica balance in Python (using libpafe)
Meta-analysis in Python
Unittest in python
Search and play YouTube videos in Python
Epoch in Python
Discord in Python
Get Google Fit API data in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
How to get a stacktrace in python
Programming in python
Plink in Python
Constant in python
Get battery level from SwitchBot in Python
Get a token for conoha in python
Get Started with TopCoder in Python (2020 Edition)
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3