Use YouTube's API to get comments on videos. I will omit the YouTube API.
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)
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.
I referred to the following article. Thank you very much.
Recommended Posts