使用 python 在 youtube 的结果中自动播放第一个视频 [英] autoplay first video in results of youtube using python

查看:11
本文介绍了使用 python 在 youtube 的结果中自动播放第一个视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个应用程序,通过输入某个关键字进行搜索,该程序会自动播放 youtube 上搜索结果中的第一个视频.如何获取搜索结果的第一个视频的链接?

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?

推荐答案

此代码用于在您提供给应用程序的搜索结果中打印第一个视频的链接.示例:-运行应用程序 .. 输入 hello it me .. 然后它会变魔术.

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]))

这篇关于使用 python 在 youtube 的结果中自动播放第一个视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆