问题
I want to make an app where I search by typing a certain keyword and the program automatically plays the first video in the search results on youtube. How do I get the link for the first video of the search result?
回答1:
this code is to prints the link of the first video on a search result you provide to the app.. example :- run the app .. type hello its me .. then it does it magic.
import urllib.request
import urllib.parse
import re
import webbrowser as wb
query_string = urllib.parse.urlencode({"search_query" : input()})
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("http://www.youtube.com/watch?v=" + search_results[0])
wb.open_new("http://www.youtube.com/watch?v={}".format(search_results[0]))
来源:https://stackoverflow.com/questions/50188882/autoplay-first-video-in-results-of-youtube-using-python