.. _search: Using the Search Feature ======================== pytubefix includes functionality to search YouTube and return results almost identical to those you would find using the search bar on YouTube's website. The integration into pytubefix means that we can directly provide you with YouTube objects that can be inspected and dowloaded, instead of needing to do additional processing. This example illustrates how the library can be used to automate YouTube searches and extract relevant data from videos:: >>> from pytubefix import Search >>> >>> results = Search('Github Issue Best Practices') >>> >>> for video in results.videos: ... print(f'Title: {video.title}') ... print(f'URL: {video.watch_url}') ... print(f'Duration: {video.length} sec') ... print('---') ... Title: Good Practices with GitHub Issues URL: https://youtube.com/watch?v=v1AeHaopAYE Duration: 406 sec --- Title: GitHub Issues Tips and Guidelines URL: https://youtube.com/watch?v=kezinXSoV5A Duration: 852 sec --- Title: 13 Advanced (but useful) Git Techniques and Shortcuts URL: https://youtube.com/watch?v=ecK3EnyGD8o Duration: 486 sec --- Title: Managing a GitHub Organization Tools, Tips, and Best Practices - Mark Matyas URL: https://youtube.com/watch?v=1T4HAPBFbb0 Duration: 1525 sec --- Title: Do you know the best way to manage GitHub Issues? URL: https://youtube.com/watch?v=OccRyzAS4Vc Duration: 534 sec --- >>> Using the Search object is really easy:: >>> from pytubefix import Search >>> s = Search('YouTube Rewind') >>> len(s.results) 17 >>> s.results [\ , \ , \ ...\ ] >>> Due to the potential for an endless stream of results, and in order to prevent a user from accidentally entering an infinite loop of requesting additional results, the ``.results`` attribute will only ever request the first set of search results. Additional results can be explicitly requested by using the ``.get_next_results()`` method, which will append any additional results to the ``.results`` attribute:: >>> s.get_next_results() >>> len(s.results) 34 >>> Additional functionality ======================== In addition to the basic search functionality which returns YouTube objects, searches also have associated autocomplete suggestions. These can be accessed as follows:: >>> s.completion_suggestions [\ 'can this video get 1 million dislikes', \ 'youtube rewind 2020 musical', \ ...\ ] The .videos method will only return the videos:: >>> from pytubefix import Search >>> >>> s = Search('YouTube Rewind') >>> >>> print(s.videos) [, , , , , , , , , , , , , , , ] >>> The .shorts method will only return the shorts.:: Here it is interesting to note that videos and shorts are from the same class of objects:: >>> from pytubefix import Search >>> >>> s = Search('YouTube Rewind') >>> >>> print(s.shorts) [, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] >>> The .playlist method will only return playlists:: >>> from pytubefix import Search >>> >>> s = Search('python tutorial') >>> >>> >>> for p in s.playlist: ... print('url', p.playlist_url) ... url https://www.youtube.com/playlist?list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU url https://www.youtube.com/playlist?list=PLsyeobzWxl7poL9JTVyndKe62ieoN-MZ3 url https://www.youtube.com/playlist?list=PLWKjhJtqVAbnqBxcdjVGgT3uVR10bzTEB url https://www.youtube.com/playlist?list=PLTjRvDozrdlxj5wgH4qkvwSOdHLOCx10f url https://www.youtube.com/playlist?list=PLBZBJbE_rGRWeh5mIBhD-hhDwSEDxogDg url https://www.youtube.com/playlist?list=PLGjplNEQ1it8-0CmoljS5yeV-GlKSUEt0 url https://www.youtube.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n url https://www.youtube.com/playlist?list=PLu0W_9lII9agwh1XjRt242xIpHhPT2llg >>> The .channel method will return only the channels:: >>> from pytubefix import Search >>> >>> s = Search('python channel') >>> >>> print(s.channel) [, , , , , , ] >>> >>> Using Filters ============= The YouTube API allows content filtering using a dictionary encoded in protobuf. Pytubefix enables the use of these filters in a simple and fast way: >>> from pytubefix.contrib.search import Search, Filter >>> >>> >>> filters = ( ... Filter.create() ... .upload_date(Filter.UploadDate.TODAY) ... .type(Filter.Type.VIDEO) ... .duration(Filter.Duration.UNDER_4_MINUTES) ... .feature([Filter.Features.CREATIVE_COMMONS, Filter.Features._4K]) ... .sort_by(Filter.SortBy.UPLOAD_DATE) ... ) >>> >>> s = Search('music', filters=filters) >>> for c in s.videos: ... print(c.watch_url) ... https://youtube.com/watch?v=_Rq8MzYz0YU https://youtube.com/watch?v=YHPGM8nBk3U https://youtube.com/watch?v=m98WShs7MLE https://youtube.com/watch?v=-vBqfC3Nir0 https://youtube.com/watch?v=LbtrnCjopwk https://youtube.com/watch?v=pfl2ga6AS3c https://youtube.com/watch?v=TzNk2ygEU4c https://youtube.com/watch?v=yQfXVRKvA70 https://youtube.com/watch?v=G5tQX990XU0 https://youtube.com/watch?v=4LQzYMhtXV8 https://youtube.com/watch?v=BOLGwdjCSAo https://youtube.com/watch?v=CgSH3Ww3MHs https://youtube.com/watch?v=_43tx98VEWc https://youtube.com/watch?v=wLDRGZaBEoQ https://youtube.com/watch?v=3qaHb2t3Lkw https://youtube.com/watch?v=56deLmbicLg https://youtube.com/watch?v=pQk2TzmwnS0 https://youtube.com/watch?v=NJ3sOlg8KGo https://youtube.com/watch?v=kfDSHjlk4Pg https://youtube.com/watch?v=8KHak4ZNO3k >>>