autoplay first video in results of youtube using python

人走茶凉 提交于 2020-11-29 09:14:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!