Extract YouTube data to CSV using YouTube API

Web scraping is extracting data from websites. It is a form of copying, in which specific data is gathered and copied from the web into a central local database or spreadsheet for later analysis or retrieval.

Image for post
Image for post

Since YouTube is the biggest video sharing website in the internet, extracting data from it can be very helpful, you can find the most popular channels, keeping track on the popularity of channels, recording likes, dislikes and views on videos and much more.

In this tutorial, you will learn how to extract data from YouTube videos using YouTube Data API v3 by google. Github.

Step 1: Activate the API key from Google Developer Console

The first thing you need to do is to extract the API key from Google Developer Console. In order to do so, you will need to activate your account for GDC with the following steps.

Create a New Project:

Image for post
Image for post
Google developer dashboard

Now Click on “New Project” as shown below and proceed.

Image for post
Image for post
Select a project window

After this, you will be automatically redirected to the Google APIs dashboard.

Image for post
Image for post
Select YouTube Data API v3 page

After that Enable the API by clicking on the Enable as shown in the below figure.

Image for post
Image for post

Now again click on the API & Services and select credentials. Navigate to the Create Credentials from the top of the page in that select API key.

Image for post
Image for post

Credentials Page

Image for post
Image for post
API key created

API key created. Keep the API handy, as you need to paste it the python code.

Step 2 — Extraction of Data from YouTube Channels

Install and import relevant libraries in Python

from apiclient.discovery import build #pip install google-api-python-client
from apiclient.errors import HttpError #pip install google-api-python-client
from oauth2client.tools import argparser #pip install oauth2client
import pandas as pd #pip install pandas

Step 3 — Set YouTube Search parameters

youTubeApiKey="AIzaSyAT7mKPxpkp66glGvzxnpryy0rOFT3r7XM" #Input your youTubeApiKeyyoutube=build('youtube','v3',developerKey=youTubeApiKey)channelId='UCt4t-jeY85JegMlZ-E5UWtA' #Input the channelID of Youtube that you want to extract data

The below snippet will convert each differently stored variable in the data frame.


And finally using the function to save the data frame to CSV file.

Once you have the data in CSV files, you can easily perform the different analyses as per project requirements.

Enjoy Scrapping!

Written by

Research aspirant in Machine learning and Data Science

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store