有时当我运行代码时,它会给我正确的输出,有时它会说“列表索引超出范围”,有时它只是继续跟随代码。我在以下位置找到了代码:https ://www.codeproject.com/articles/873060/python-search-youtube-for-video
我怎样才能解决这个问题?
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()