Using YouTube Data API v3 with Python — Search Videos by the keyword and get the video details

Maurya Allimuthu
4 min readSep 19, 2019

--

I want to share my coding experience on how to get the list of videos by searching with a keyword via Youtube Data API v3. Also how to get the individual video details.

Step 1 — Register in google developer site
Register and login in https://console.developers.google.com/

Step 2 — Create Project
On the right corner you can see the “New Project” button. If you have already created it will appear on the separate window. The below picture will help you out.

Picture :: New Project

Step 3— Search YouTube Data API v3
You can click on the Library on the left side or select the apt project in the dropdown and search for “data api”

Search by “data api” and click the icon.

Click “MANAGE” to go to next step.

Step 4 — Create Credentials
Once “MANAGE” is clicked you will be navigated to a place where you can see “CREATE CREDENTIALS”

Click to create credentials.

While creating credentials you will see the below options, choose the apt one. I have choose “Other non-UI (e.g cron job, daemon)”.

Finally, you will be seeing the picture on the left side with the new API KEY generated/created.

Step 4 — packages needed
The packages needed are google-api-python-client https://pypi.org/project/google-api-python-client/

Step 5 — configuration of the API
Please find do the below steps to use the API functionalities.

api_key='A*************************************M'
youtube = discovery.build('youtube', 'v3', developerKey=api_key)

Step 6— search the youtube videos based on a keyword
Please find do the below steps to get the list of videos. Please note the maxResults can be set to maximum of 50.

req = youtube.search().list(q='machine learning tutorial', part='snippet', type='video', maxResults=50, pageToken=None)
res = req.execute()

If you want to get all the videos, then you need to loop through using the pageToken attribute.

MAX_COUNT = 50
nextPageToken = None
search_by = 'machine learning tutorial'
While True:
req = youtube.search().list(q=search_by, part='snippet', type='video', maxResults=MAX_COUNT, pageToken=nextPageToken)
res = req.execute()
nextPageToken = res['nextPageToken']
items = res['items']
if res['nextPageToken'] == None:
break; # exit from the loop
for each_item in items:
#store in DB or file or print the same.
print (each_item)

Step 6 — Get the individual video details like ( duration and description)
You need to use the semantics like below. By specifying part as ‘snippet’ you can get the description and specifying the part as ‘contentDetails’ you can get the duration of the video.

videoid = 'JMUxmLyrhSk' #example
req2 = youtube.videos().list(part='snippet,contentDetails', id=videoid)
res2 = req2.execute()

For more information, refer the below link.

Additionally, To convert the duration (example: P2DT1S , PT2H12M51S) of the video to seconds refer https://stackoverflow.com/questions/15596753/how-do-i-get-video-durations-with-youtube-api-version-3/58004773#58004773

Best wishes

--

--

Responses (1)