In this article, we shall see how to search youtube with python and get a JSON response with a link to the videos, thumbnails, etc. All these are possible with the help of a python library called youtube-search-python. Let us get started by installing the library.
This package can be installed easily using pip as,
pip install youtube-search-python
You can also clone the repo from here and run the following command on the terminal inside the downloaded or cloned directory.
python setup.py install
Once the package is installed we are good to go.
Made without using YouTube Data API v3 and any other third-party library. This library is intended for personal and non-commercial usage only.
We can use this library to search for both music videos and playlists. We can also specify the number of results the search function should return.
Each search result will return the following details.
- thumbnails url
Let us look at the example code for each of music video search and also playlist search.
Music video search
The following code snippet shows you how to search for music videos using python. We have to import the SearchVideos class from the youtubesearchpython module.
This will take the name, offset value, response mode and maximum results per search results as the arguments.
The output of the above code snippet is,
Playlist search is also similar to this one. We import the SearchPlaylists class from youtubesearchpython module. This class also takes the same arguments as the SearchVideos class.
But the response of the SearchPlaylists class is different. The following data are contained in the response of this class.
The response of the above code snippet is,
This library is pretty much straight-forward and easy to use compared to other libraries. It also returns all the necessary details.