Empty list most of the time outputted when trying to find first link when getting links from youtube (Python)

Viewed 72

I followed this tutorial from this website in order to learn how I can extract the first link from youtube based on a given query. I have implemented the code into a function like so:

import urllib.request
import re
def GetBestYoutubeLink(MusicRequest):
         MusicSearchLink = MusicRequest.replace(" ","+")
         MusicSearchLink = "https://www.youtube.com/results?search_query=" + MusicSearchLink
         HTMLContent = urllib.request.urlopen(MusicSearchLink)
         SearchResults = re.findall(r'href=\"\/watch\?v=(.{11})', HTMLContent.read().decode())
         print(SearchResults)
         BestLink = "http://www.youtube.com/embed/" + SearchResults[0]
         return BestLink

Where a query will passed into the function and it would print the first/best url. However the problem I am facing from this solution is most of the time the SearchResults array when printed is empty and hence I am unable to get the first url. It is not like the query is an uncommon query as I had tried popular songs and videos to obtain the link of, but it simply returns as empty, however it works sometimes with the correct output of the best link. In order to find a solution to this I gave the following statement between when it prints the SearchResults array and when the BestLink variable is defined:

    if SearchResults == []:
        print(SearchResults)
        MusicPlayer(MusicRequest)

Where if the SearchResults array is empty then it runs the function again. However it is being rerun and an empty list is being printed sometimes 20 to 30 times which is not at all efficient. I would like to understand what may the problem be behind my list returning as empty most of the time but sometimes is populated and hence am able to get the link and how may I be able to fix this?

My current python version is 3.6 and I am running on macOS Catalina.

1 Answers

I think the style of the query return changed since this tutorial has been written. If you print the HTMLContent.read().decode() you can see that the URLs are in form "url":"/watch?v=0755SXCTCN0"

I changed your code, you also had a search_results[0] which doesn't exist.

import urllib.request
import re

def GetBestYoutubeLink(MusicRequest):
         MusicSearchLink = MusicRequest.replace(" ","+")
         MusicSearchLink = "https://www.youtube.com/results?search_query=" + MusicSearchLink
         HTMLContent = urllib.request.urlopen(MusicSearchLink)
         SearchResults = re.findall(r'/watch\?v=(.{11})', HTMLContent.read().decode())
         print(SearchResults)
         BestLink = "http://www.youtube.com/embed/" + SearchResults[0]
         return BestLink
Related