It is recommended to install this module by using pip:

pip install youtube-data-api


import os
import pandas as pd
from youtube_api import YoutubeDataApi

In order to access the API, you’ll need to get a service key from the Google Cloud Console . I like to store my API keys as environment variables in ~/.bash_profile so that I don’t have to hard code them.:

YT_KEY = os.environ.get('YOUTUBE_API_KEY') # you can hardcode this, too.
yt = YouTubeDataAPI(YT_KEY)

We now have created a YouTubeDataAPI class as yt, which can be used to make API calls, such as searching for the most relevant videos of Alexandra Ocasio-Cortez.

searches ='alexandria ocasio-cortez',
{'video_id': 'LlillsHgcaw',
 'channel_title': 'Fox News',
 'channel_id': 'UCXIJgqnII2ZOINSWNOGFThA',
 'video_publish_date': datetime.datetime(2019, 2, 19, 4, 57, 51),
 'video_title': 'Rep. Alexandria Ocasio-Cortez taken to task by fellow progressives',
 'video_description': 'New York City Mayor Bill de Blasio criticizes Alexandria Ocasio-Cortez over her opposition to the Amazon deal.',
 'video_category': None,
 'video_thumbnail': '',
 'collection_date': datetime.datetime(2019, 2, 20, 14, 48, 19, 487877)}

All API requests are parsed from raw JSON into dictionaries.

Typically an API call returns a list of dictionary objects. This is perfect for converting into Pandas DataFrames, or saving as JSON.

df_search = pd.DataFrame(searches)
channel_id channel_title collection_date video_category video_description video_id video_publish_date video_thumbnail video_title
0 UCXIJgqnII2ZOINSWNOGFThA Fox News 2019-02-20 14:49:34.262643 None New York City Mayor Bill de Blasio criticizes ... LlillsHgcaw 2019-02-19 04:57:51 Rep. Alexandria Ocasio-Cortez taken to task by...
1 UCXIJgqnII2ZOINSWNOGFThA Fox News 2019-02-20 14:49:34.262672 None Alexandria Ocasio-Cortez's new environmental m... 3EazY4bw6u8 2019-02-19 00:34:22 Critics mock Ocasio-Cortez's Green New Deal ro...
2 UCeY0bbntWzzVIaj2z3QigXg NBC News 2019-02-20 14:49:34.262693 None Newly-elected Rep. Alexandria Ocasio-Cortez (D... 8YH0t3H1Y_Y 2019-02-16 21:40:10 Rep. Ocasio-Cortez Defends Green New Deal In I...
3 UCnsvJeZO4RigQ898WdDNoBw EL PAIS 2019-02-20 14:49:34.262713 None Alexandria Ocasio-Cortez jura en Nueva York su... wAmEYOcnu_g 2019-02-17 11:03:09 ALEXANDRIA OCASIO-CORTEZ: "Sed valientes con n...
4 UCJg9wBPyKMNA5sRDnvzmkdg FOX 10 Phoenix 2019-02-20 14:49:34.262733 None President Donald Trump is expected to urge Ven... VhEo5sm5Eu4 2019-02-18 22:20:22 NO SOCIALISM: President Trump Takes On Alexand...

Aside from the default parser, the parse argument allows users to create custom functions to parse and process API resonses. You can also get raw JSON from the API by using the youtube_api.parsers.raw_json() parser, or setting parser to None.'alexandria ocasio-cortez',
[{'kind': 'youtube#searchResult',
  'etag': '"XI7nbFXulYBIpL0ayR_gDh3eu1k/iwS8DlBT9x9lWSRCq4JFPMR-Z00"',
  'id': {'kind': 'youtube#video', 'videoId': 'byc_lBOY_rI'},
  'snippet': {'publishedAt': '2018-07-31T18:52:29.000Z',
   'channelId': 'UCZaT_X_mc0BI-djXOlfhqWQ',
   'title': "Who's Afraid Of Alexandria Ocasio-Cortez? Everyone (HBO)",
   'description': "Alexandria Ocasio-Cortez shocked Democrats when she won a New York City primary over one of the party's entrenched leaders. Her next chapter is likely to be ...",
   'thumbnails': {'default': {'url': '',
     'width': 120,
     'height': 90},
    'medium': {'url': '',
     'width': 320,
     'height': 180},
    'high': {'url': '',
     'width': 480,
     'height': 360}},
   'channelTitle': 'VICE News',
   'liveBroadcastContent': 'none'}}]

youtube_api.parsers are intended to allow customized data parsing for those who want it, with robust defaults for less advanced users.