{"id":18068,"date":"2022-09-02T12:58:13","date_gmt":"2022-09-02T07:28:13","guid":{"rendered":"https:\/\/copyassignment.com\/?p=18068"},"modified":"2022-12-06T13:31:46","modified_gmt":"2022-12-06T08:01:46","slug":"how-to-extract-youtube-comments-using-python","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/how-to-extract-youtube-comments-using-python\/","title":{"rendered":"How to extract YouTube comments using Python?"},"content":{"rendered":"\n<p>Today, we will learn how to extract YouTube comments using Python with the help of google APIs. Extracting comments from youtube can be a daunting task. But with the help of google API, it can be made very simple. All you need is a google API key for youtube data. Need source code? <a href=\"#code\">Click here<\/a> to copy the code to extract YouTube comments.<\/p>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started<\/h2>\n\n\n\n<p>Let&#8217;s view the steps to get the API key:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go to https:\/\/console.cloud.google.com\/<\/li>\n\n\n\n<li>Create a project<\/li>\n\n\n\n<li>Click on APIs and services<\/li>\n\n\n\n<li>Click on enable APIs and services<\/li>\n\n\n\n<li>Search for youtube data API v3<\/li>\n\n\n\n<li>Click on this <\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-1024x106.png\" alt=\"extract YouTube comments using Python with Google's YouTube API\" class=\"wp-image-18085 lazyload\" width=\"1062\" height=\"109\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-1024x106.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-300x31.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-768x80.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-1026x106.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-675x70.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image.png 1244w\" data-sizes=\"(max-width: 1062px) 100vw, 1062px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1062px; --smush-placeholder-aspect-ratio: 1062\/109;\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Enable this API<\/li>\n\n\n\n<li>Go to credentials and click create new credentials<\/li>\n\n\n\n<li>Select API key<\/li>\n\n\n\n<li>You will get your API key. Copy this and save it for future use.<\/li>\n<\/ul>\n\n\n\n<p><strong>Now let&#8217;s start building the project.<\/strong><\/p>\n\n\n\n<p>You will need to install the google API client library for python. Run the below command on your terminal<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Approach to Extract YouTube Comments<\/h2>\n\n\n\n<p>Youtube does not display all of its comments at once. We get comments in groups of 20 by default. To get further comments we need to look at the next page token that we get while querying the comments. This can be used to get the next batch of comments and so on. <\/p>\n\n\n\n<p>We create 2 recursive functions for our task. One is get_comments which finds all the top-level comments. Another is get_replies to get all the replies or the entire comment thread of a particular comment.<\/p>\n\n\n\n<p>You need to enter your API key in the given field. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"code\">Python Code to Extract YouTube Comments<\/h2>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">from googleapiclient.discovery import build\n\nvideo_id = \"CfttIk4Yjqg\"\napi_key = '&lt;Enter your api key here>'\n\n# recursive function to get all replies in a comment thread\ndef get_replies(comment_id, token):\n    replies_response = yt_object.comments().list(part = 'snippet', maxResults = 100, parentId = comment_id, pageToken = token).execute()\n\n    for reply in replies_response['items']:\n        all_comments.append(reply['snippet']['textDisplay'])\n\n    if replies_response.get(\"nextPageToken\"):\n        return get_replies(comment_id, replies_response['nextPageToken'])\n    else:\n        return []\n\n\n# recursive function to get all comments\ndef get_comments(youtube, video_id, next_view_token):\n    global all_comments\n\n    # check for token\n    if len(next_view_token.strip()) == 0:\n        all_comments = []\n\n    if next_view_token == '':\n        # get the initial response\n        comment_list = youtube.commentThreads().list(part = 'snippet', maxResults = 100, videoId = video_id, order = 'relevance').execute()\n    else:\n        # get the next page response\n        comment_list = youtube.commentThreads().list(part = 'snippet', maxResults = 100, videoId = video_id, order='relevance', pageToken=next_view_token).execute()\n    # loop through all top level comments\n    for comment in comment_list['items']:\n        # add comment to list\n        all_comments.append([comment['snippet']['topLevelComment']['snippet']['textDisplay']])\n        # get number of replies\n        reply_count = comment['snippet']['totalReplyCount']\n        all_replies = []\n        # if replies greater than 0\n        if reply_count > 0:\n            # get first 100 replies\n            replies_list = youtube.comments().list(part='snippet', maxResults=100, parentId=comment['id']).execute()\n            for reply in replies_list['items']:\n                # add reply to list\n                all_replies.append(reply['snippet']['textDisplay'])\n\n            # check for more replies\n            while \"nextPageToken\" in replies_list:\n                token_reply = replies_list['nextPageToken']\n                # get next set of 100 replies\n                replies_list = youtube.comments().list(part = 'snippet', maxResults = 100, parentId = comment['id'], pageToken = token_reply).execute()\n                for reply in replies_list['items']:\n                    # add reply to list\n                    all_replies.append(reply['snippet']['textDisplay'])\n\n        # add all replies to the comment\n        all_comments[-1].append(all_replies)\n\n    if \"nextPageToken\" in comment_list:\n        return get_comments(youtube, video_id, comment_list['nextPageToken'])\n    else:\n        return []\n\n\nall_comments = []\n\n# build a youtube object using our api key\nyt_object = build('youtube', 'v3', developerKey=api_key)\n\n# get all comments and replies\ncomments = get_comments(yt_object, video_id, '')\n\nfor comment, replies in all_comments:\n    print(comment)\n    if len(replies) > 0:\n        print(\"There are\", len(replies), \"replies\")\n        print(\"\\tReplies:\")\n        for reply in replies:\n            print(\"\\t\" + reply)\n    print()<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Output:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Vir is actually a philosopher disguised as a comedian because if not with humor, how else will the world understand bitter truths? &lt;br&gt;This was beyond amazing!!! &amp;lt;33\n\nWhen he started with the monkey and scientists, I thought, &amp;quot;This is gonna be a boring one. &amp;quot;.... But oh maan, you rocked it!! \ud83e\udd75\ud83d\udd25\ud83d\udcaf\ud83d\udcaf\nThere are 3 replies\n\tReplies:\n\tWell i was going to skip the video&lt;br&gt;&lt;br&gt;But your comment made me stay&lt;br&gt;And i must stay&lt;br&gt;It was worth it\n\t@Sridhar C Me 2 \ud83d\ude09\n\tMust admit... I was confused for a while there too\n\nHe just said a lot and expressed a lot, only if one really understands \ud83d\udc4c\ud83c\udffb\ud83d\udc4c\ud83c\udffb\ud83d\udc4f\ud83c\udffb\ud83d\udc4f\ud83c\udffb\n\nThis guy can do both Delhi Belly and a set on Indian Feminism, incredible range man.\nThere are 3 replies\n\tReplies:\n\tTIL i realised that was vir das lmao\ud83d\ude02\ud83d\ude02\ud83d\ude02achcha hua ye comment padha\n\t@Snehil Vishwakarma just reading that movies name has ruined my day\n\tI think a better comparison would be Mastizaade and this set\n\nThis is Hard Slap Of Reality Check With Flavour of Humor!!!! \ud83d\udd25\ud83d\udcaf<\/code><\/pre>\n\n\n\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-9886351916045880\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-9886351916045880\"\n     data-ad-slot=\"7933252109\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<div style=\"text-align:center\" class=\"wp-block-atomic-blocks-ab-button ab-block-button\"><a href=\"https:\/\/copyassignment.com\/top-100-python-projects-with-source-code\/\" class=\"ab-button ab-button-shape-rounded ab-button-size-medium\" style=\"color:#ffffff;background-color:#3373dc\">Best 100+ Python Projects with source code<\/a><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/create-your-own-chatgpt-with-python\/\">Create your own ChatGPT with\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/bakery-management-system-in-python-class-12-project\/\">Bakery Management System in Python | Class 12 Project<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sqlite-crud-operations-in-python\/\">SQLite | CRUD Operations in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/event-management-system-project-in-python\/\">Event Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ticket-booking-and-management-in-python\/\">Ticket Booking and Management in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hostel-management-system-project-in-python\/\">Hostel Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sales-management-system-project-in-python\/\">Sales Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/bank-management-system-project-in-cpp\/\">Bank Management System Project in C++<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-download-file-from-url-4-methods\/\">Python Download File from URL | 4 Methods<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-programming-examples-fundamental-programs-in-python\/\">Python Programming Examples | Fundamental Programs in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/spell-checker-in-python\/\">Spell Checker in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/portfolio-management-system-in-python\/\">Portfolio Management System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/stickman-game-in-python\/\">Stickman Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/contact-book-project-in-python\/\">Contact Book project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/loan-management-system-project-in-python\/\">Loan Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/cab-booking-system-in-python\/\">Cab Booking System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/brick-breaker-game-in-python\/\">Brick Breaker Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tank-game-in-python\/\">Tank game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-piano-in-python\/\">GUI Piano in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ludo-game-in-python\/\">Ludo Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/rock-paper-scissors-game-in-python\/\">Rock Paper Scissors Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/snake-and-ladder-game-in-python\/\">Snake and Ladder Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/puzzle-game-in-python\/\">Puzzle Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/medical-store-management-system-project-in-python\/\">Medical Store Management System Project in\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/creating-dino-game-in-python\/\">Creating Dino Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tic-tac-toe-game-in-python\/\">Tic Tac Toe Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/test-typing-speed-using-python-app\/\">Test Typing Speed using Python App<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/moviepy-python-video-editing-library\/\">MoviePy: Python Video Editing Library<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/scientific-calculator-in-python-2\/\">Scientific Calculator in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-to-do-list-app-in-python-tkinter\/\">GUI To-Do List App in Python Tkinter<\/a><\/li>\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Today, we will learn how to extract YouTube comments using Python with the help of google APIs. Extracting comments from youtube can be a daunting&#8230;<\/p>\n","protected":false},"author":62,"featured_media":18137,"comment_status":"closed","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1403],"tags":[],"class_list":["post-18068","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allcategorites","category-python-projects","wpcat-22-id","wpcat-1403-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/18068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=18068"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/18068\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media\/18137"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=18068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=18068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=18068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}