Sometimes when I run the code it gives me the correct output, other times it says "List index out of range" and other times it just continues following code. I found the code on: https://www.codeproject.com/articles/873060/python-search-youtube-for-video
How can I fix this?
searchM = input("Enter the movie you would like to search: ")
def watch_trailer():
query_string = urllib.parse.urlencode({"search_query" : searchM})
html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
print("Click on the link to watch the trailer: ","http://www.youtube.com/watch?v="+search_results[0])
watch_trailer()