1 Star 0 Fork 0

wdc/Python001

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
youtube.py 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
Mary 提交于 2020-03-15 17:53 . search_with_artist
'''
Author: Abhinav Anand
git: github.com/ab-anand
mail: abhinavanand1905@gmail.com
Requirements: requests, BeautifulSoupd
'''
import webbrowser
import requests
from bs4 import BeautifulSoup
'''
headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}
'''
input_func = None
try:
input_func = raw_input('Enter the song to be played: ')
input_func+=raw_input('Enter artist name: ')
except NameError:
input_func = input('Enter the song to be played: ')
input_func+=input('Enter artist name: ')
query = input_func.replace(' ', '+')
# search for the best similar matching video
url = 'https://www.youtube.com/results?search_query=' + query
source_code = requests.get(url, timeout=15)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
# fetches the url of the video
songs = soup.findAll('div', {'class': 'yt-lockup-video'})
song = songs[0].contents[0].contents[0].contents[0]
# link = song['href']
# webbrowser.open('https://www.youtube.com' + link)
try:
link = song['href']
webbrowser.open('https://www.youtube.com' + link)
except KeyError:
print("Can't find any song,check your network or try a new word")
# hey i'm learning git.
# i welcome you too to come and learn
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ijiwei/Python001.git
git@gitee.com:ijiwei/Python001.git
ijiwei
Python001
Python001
master

搜索帮助