大一小学期作业是用pyqt5和爬虫写一个小程序,正巧最近无聊歌荒,就想着能不能写一个听音乐的小软件.那么就从0到成品以文章形式发出来.ui设计会用到qtdesigner,拖拉模块就能完成布局,简单的很.总体代码也不难,有一点python基础都能看懂.而且UI代码和功能代码分离,方便更改.
大概参考下布局
左面功能栏,右面展示信息,上方提供搜索功能.那么我想实现的功能便是获取热榜,获取热门歌单,给定歌单链接解析歌单,随机推荐热榜歌曲,播放本地音乐.
采用无功能栏窗口
到此我们的界面大体上完工了,下面介绍如何实现按钮功能,以及图片的显示
热歌榜url为:前缀+3778678
通过检查元素我们可以找到歌曲名称以及歌曲id,注意!如果不删掉url中/#则无法爬取
url='https://music.163.com/discover/toplist?id=3778678'
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=html.xpath('//a[contains(@href,"song?")]')
id_list=id_list[0:-11]
for id in id_list:
href=id.xpath('./@href')[0]
song_id=href.split('=')[1]
hotsongid.append(song_id)
song_name=id.xpath('./text()')[0]
hotsongname.append(song_name)
songandsinger=dict(zip(hotsongname,hotsongid))
现在解释一下代码,开头没什么好说的,通过requests获得网页源码,再用xpath解析.看一下审查元素里的内容,歌曲id放在a标签的href里,同时b被包含在a标签里,直接用text()方法便可获取其中文本内容.对idlist进行切片是因为后面有些无关内容,需要删掉.而song_id=href.split(’=’)[1]的意思是分割字符串,获取到=后面的纯数字id.最后得到一个字典,里面包含着名字以及id.至于为什么是字典稍后说明
先说下大体思路:获取字典中的键并组成列表,再随机抽出四个歌曲名,再用歌曲名获得id,访问拼接好的完整id.再寻找歌曲封面,用pil压缩图片
url='https://music.163.com/discover/toplist?id=3778678'
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=html.xpath('//a[contains(@href,"song?")]')
id_list=id_list[0:-11]
for id in id_list:
href=id.xpath('./@href')[0]
song_id=href.split('=')[1]
hotsongid.append(song_id)
song_name=id.xpath('./text()')[0]
hotsongname.append(song_name)
songandsinger=dict(zip(hotsongname,hotsongid))
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/discover/toplist?id=3778678')
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//div[contains(@class,"text")]')
time1=wd.find_elements_by_xpath('//td[contains(@class," s-fc3")]')
for i in elements:
hotsinger.append(i.get_attribute('title'))
for i in time1:
hotsongtime.append(i.get_attribute('textContent')[:-2])
wd.quit()
#
print(hotsongname[0])
# 随机获取四首歌并获取封面.
for i in songandsinger.keys():
randomsongname.append(i)
randomsongname= random.sample(randomsongname, 4)
print(randomsongname)
for j in randomsongname:
randomsongid.append('https://music.163.com/song?id='+songandsinger[j])
for i in songandsinger.keys():
hotsongname.append(i)
for i in songandsinger.values():
hotsongid.append(i)
def getrandompicture(randomsongid):
temp=1
for k in randomsongid:
url=k
print(url)
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('picture{}.jpg'.format(temp),'wb') as f:
f.write(content)
img_path = Image.open('picture{}.jpg'.format(temp))
img_size = img_path.resize((150, 150))
img_size.save('picture{}.jpg'.format(temp),'png')
temp=temp+1
getrandompicture(randomsongid)
解释一下代码,
审查元素看一下图片地址,有两个一个是缩略图一个是高清大图,那我们先选择大图,我搜了下含有data-src的只有这个一个,于是xpath可以写成’//img/@data-src’,至于为什么切片
看出区别了吗?
一个是[‘http://p2.music.126.net/6EX0yj-r5GBLzSNU20rrmQ==/109951165791933542.jpg’]
另一个是http://p2.music.126.net/6EX0yj-r5GBLzSNU20rrmQ==/109951165791933542.jpg
如果不切片直接传入则会报错.
数据的保存分为两个部分,一个是永久保存(包含热榜歌曲的歌名,id,时长,歌手,以及热门歌单id,便于后续展示以及对界面重置.)另一个是临时保存,包含解析歌单的内容以及搜索歌曲获得的内容,会被随时更改.
import requests
from lxml import etree
import random
import sys
import unicodedata
from PIL import Image
from PyQt5.QtWidgets import QApplication,QMainWindow,QFileDialog, QProgressBar,QStyleFactory
from PyQt5.Qt import QIcon,QSize
from PyQt5.QtCore import Qt, QBasicTimer
from PyQt5.Qt import *
from functools import partial
from PyQt5 import QtCore, QtWidgets
from selenium import webdriver
import time
from PyQt5.QtCore import QTimer
from Ui_作业主程序 import Ui_Form as A_Ui
from Ui_getlisturl import Ui_Form as B_Ui
from Ui_error import Ui_Form as C_Ui
import os
from PyQt5 import QtMultimedia
from PyQt5.QtCore import QUrl
randomsongname=[]
randomsongid=[]
count=0
# 永久保存
hotsongname=[]
hotsongid=[]
hotsinger=[]
hotsongtime=[]
hotlistid=[]
#
# 临时保存
tempname=[]
tempid=[]
tempsinger=[]
tempsongtime=[]
#
# 获取热榜歌名,歌手,id,时长.
url='https://music.163.com/discover/toplist?id=3778678'
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=html.xpath('//a[contains(@href,"song?")]')
id_list=id_list[0:-11]
for id in id_list:
href=id.xpath('./@href')[0]
song_id=href.split('=')[1]
hotsongid.append(song_id)
song_name=id.xpath('./text()')[0]
hotsongname.append(song_name)
songandsinger=dict(zip(hotsongname,hotsongid))
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/discover/toplist?id=3778678')
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//div[contains(@class,"text")]')
time1=wd.find_elements_by_xpath('//td[contains(@class," s-fc3")]')
for i in elements:
hotsinger.append(i.get_attribute('title'))
for i in time1:
hotsongtime.append(i.get_attribute('textContent')[:-2])
wd.quit()
#
print(hotsongname[0])
# 随机获取四首歌并获取封面.
for i in songandsinger.keys():
randomsongname.append(i)
randomsongname= random.sample(randomsongname, 4)
print(randomsongname)
for j in randomsongname:
randomsongid.append('https://music.163.com/song?id='+songandsinger[j])
for i in songandsinger.keys():
hotsongname.append(i)
for i in songandsinger.values():
hotsongid.append(i)
def getrandompicture(randomsongid):
temp=1
for k in randomsongid:
url=k
print(url)
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('picture{}.jpg'.format(temp),'wb') as f:
f.write(content)
img_path = Image.open('picture{}.jpg'.format(temp))
img_size = img_path.resize((150, 150))
img_size.save('picture{}.jpg'.format(temp),'png')
temp=temp+1
getrandompicture(randomsongid)
#
#
class AUi(QtWidgets.QMainWindow, A_Ui):
def __init__(self):
super(AUi, self).__init__()
self.setupUi(self)
self.player = QtMultimedia.QMediaPlayer()
self.player.setVolume(50.0)
self.picturename1.setText(randomsongname[0])
self.picturename2.setText(randomsongname[1])
self.picturename3.setText(randomsongname[2])
self.picturename4.setText(randomsongname[3])
self.gethotmusicbutton.clicked.connect(self.restore)
self.progressBar.setTextVisible(False)
self.progressBar.setValue(0)
self.stoptoolbutton.clicked.connect(self.stopplay)
# self.pushButton.setFlat(True)
self.gethotlistbutton.clicked.connect(self.gethotlist)
self.inputsongnamebutton.clicked.connect(self.searchmusic)
self.parsesonglistbutton.clicked.connect(lambda:{
b.show()})
self.picture1toolbutton1.clicked.connect(partial(self.parselist1,0))
self.picture1toolbutton2.clicked.connect(partial(self.parselist1,1))
self.picture1toolbutton3.clicked.connect(partial(self.parselist1,2))
self.picture1toolbutton4.clicked.connect(partial(self.parselist1,3))
self.playmusicbutton1.clicked.connect(partial(self.playselectmusic,0))
self.playmusicbutton2.clicked.connect(partial(self.playselectmusic,1))
self.playmusicbutton3.clicked.connect(partial(self.playselectmusic,2))
self.playmusicbutton4.clicked.connect(partial(self.playselectmusic,3))
self.playmusicbutton5.clicked.connect(partial(self.playselectmusic,4))
self.playmusicbutton6.clicked.connect(partial(self.playselectmusic,5))
self.playmusicbutton7.clicked.connect(partial(self.playselectmusic,6))
self.playmusicbutton8.clicked.connect(partial(self.playselectmusic,7))
self.playmusicbutton9.clicked.connect(partial(self.playselectmusic,8))
self.playmusicbutton10.clicked.connect(partial(self.playselectmusic,9))
self.picture1toolbutton1.setIconSize(QSize(150, 150))
self.picture1toolbutton1.setIcon(QIcon("picture1.jpg"))
self.stoptoolbutton.setIconSize(QSize(50, 50))
self.stoptoolbutton.setIcon(QIcon("暂停.jpg"))
self.picture1toolbutton2.setIconSize(QSize(150, 150))
self.picture1toolbutton2.setIcon(QIcon("picture2.jpg"))
self.picture1toolbutton3.setIconSize(QSize(150, 150))
self.picture1toolbutton3.setIcon(QIcon("picture3.jpg"))
self.picture1toolbutton4.setIconSize(QSize(150, 150))
self.picture1toolbutton4.setIcon(QIcon("picture4.jpg"))
self.picture1toolbutton1.setToolTip(randomsongname[0])
self.picture1toolbutton2.setToolTip(randomsongname[1])
self.picture1toolbutton3.setToolTip(randomsongname[2])
self.picture1toolbutton4.setToolTip(randomsongname[3])
self.showidlaber1.setText(hotsongid[0])
self.showidlaber2.setText(hotsongid[1])
self.showidlaber3.setText(hotsongid[2])
self.showidlaber4.setText(hotsongid[3])
self.showidlaber5.setText(hotsongid[4])
self.showidlaber6.setText(hotsongid[5])
self.showidlaber7.setText(hotsongid[6])
self.showidlaber8.setText(hotsongid[7])
self.showidlaber9.setText(hotsongid[8])
self.showidlaber10.setText(hotsongid[9])
self.showsongnamelaber1.setText(hotsongname[0])
self.showsongnamelaber2.setText(hotsongname[1])
self.showsongnamelaber3.setText(hotsongname[2])
self.showsongnamelaber4.setText(hotsongname[3])
self.showsongnamelaber5.setText(hotsongname[4])
self.showsongnamelaber6.setText(hotsongname[5])
self.showsongnamelaber7.setText(hotsongname[6])
self.showsongnamelaber8.setText(hotsongname[7])
self.showsongnamelaber9.setText(hotsongname[8])
self.showsongnamelaber10.setText(hotsongname[9])
self.showsingerlaber1.setText(hotsinger[0])
self.showsingerlaber2.setText(hotsinger[1])
self.showsingerlaber3.setText(hotsinger[2])
self.showsingerlaber4.setText(hotsinger[3])
self.showsingerlaber5.setText(hotsinger[4])
self.showsingerlaber6.setText(hotsinger[5])
self.showsingerlaber7.setText(hotsinger[6])
self.showsingerlaber8.setText(hotsinger[7])
self.showsingerlaber9.setText(hotsinger[8])
self.showsingerlaber10.setText(hotsinger[9])
self.showtimelaber1.setText(hotsongtime[0])
self.showtimelaber2.setText(hotsongtime[1])
self.showtimelaber3.setText(hotsongtime[2])
self.showtimelaber4.setText(hotsongtime[3])
self.showtimelaber5.setText(hotsongtime[4])
self.showtimelaber6.setText(hotsongtime[5])
self.showtimelaber7.setText(hotsongtime[6])
self.showtimelaber8.setText(hotsongtime[7])
self.showtimelaber9.setText(hotsongtime[8])
self.showtimelaber10.setText(hotsongtime[9])
self.setWindowOpacity(0.95)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
a = AUi()
a.show()
#b = BUi()
sys.exit(app.exec_())
现在框架搭建完成,可以添加肉了.
name=[]
id=[]
from selenium import webdriver
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/#/playlist?id=6702371651')
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//a[contains(@href,"/song?id")]')
for i in elements:
print(i.get_attribute('href'))
# id.append(i.get_attribute('href'))
elements2 =wd.find_elements_by_xpath('//b[@title]')
for i in elements2:
print(i.get_attribute('title'))
# name.append(i.get_attribute('title'))
# 2.
name=[]
id=[]
from selenium import webdriver
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/#/playlist?id=6702371651')
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//a[contains(@href,"/song?id")]')
for i in elements:
# print(i.get_attribute('href'))
id.append(i.get_attribute('href'))
elements2 =wd.find_elements_by_xpath('//b[@title]')
for i in elements2:
# print(i.get_attribute('title'))
name.append(i.get_attribute('title'))
print(name)
print(id)
name=[]
id=[]
singer=[]
songtime=[]
from selenium import webdriver
import unicodedata
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/#/playlist?id=6702371651')
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//a[contains(@href,"/song?id")]')
for i in elements:
id.append(i.get_attribute('href')[30:])
elements2 =wd.find_elements_by_xpath('//b[@title]')
for i in elements2:
s=i.get_attribute('title')
temp=unicodedata.normalize('NFKC', str(s))
name.append(temp)
elements3 =wd.find_elements_by_xpath('//div[contains(@class,"text")]')
for i in elements3:
singer.append(i.get_attribute('title'))
for i in range(1,11):
if singer[i]=='':
singer.pop(i)
time=wd.find_elements_by_xpath('//td[contains(@class," s-fc3")]')
for i in time:
songtime.append(i.get_attribute('textContent')[:-2])
print(name)
print(id)
print(singer)
print(songtime)
wd.quit()
在大佬指导下成功解决.
代码部分解决了,现在切换到图形界面.
设计一个新界面,取名为getlisturl.ui
先写一个信号槽,用处是点击解析按钮时传递用户输入的文本信息到写好的解析歌单函数,并关闭自身
#
class BUi(QtWidgets.QMainWindow, B_Ui):
def __init__(self):
super(BUi, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.parselist)
# 解析歌单
def parselist(self):
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get(self.geturllineedit.text())
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//a[contains(@href,"/song?id")]')
global tempname
global tempid
global tempsinger
global tempsongtime
tempname=[]
tempid=[]
tempsinger=[]
tempsongtime=[]
for i in elements:
tempid.append(i.get_attribute('href')[30:])
elements2 =wd.find_elements_by_xpath('//b[@title]')
for i in elements2:
s=i.get_attribute('title')
temp=unicodedata.normalize('NFKC', str(s))
tempname.append(temp)
elements3 =wd.find_elements_by_xpath('//div[contains(@class,"text")]')
for i in elements3:
tempsinger.append(i.get_attribute('title'))
for i in range(1,11):
if tempsinger[i]=='':
tempsinger.pop(i)
time2=wd.find_elements_by_xpath('//td[contains(@class," s-fc3")]')
for i in time2:
tempsongtime.append(i.get_attribute('textContent')[:-2])
print(tempid)
print(tempname)
wd.quit()
b.close()
a.showsongnamelaber1.setText(tempname[0])
a.showsongnamelaber2.setText(tempname[1])
a.showsongnamelaber3.setText(tempname[2])
a.showsongnamelaber4.setText(tempname[3])
a.showsongnamelaber5.setText(tempname[4])
a.showsongnamelaber6.setText(tempname[5])
a.showsongnamelaber7.setText(tempname[6])
a.showsongnamelaber8.setText(tempname[7])
a.showsongnamelaber9.setText(tempname[8])
a.showsongnamelaber10.setText(tempname[9])
a.showidlaber1.setText(tempid[0])
a.showidlaber2.setText(tempid[1])
a.showidlaber3.setText(tempid[2])
a.showidlaber4.setText(tempid[3])
a.showidlaber5.setText(tempid[4])
a.showidlaber6.setText(tempid[5])
a.showidlaber7.setText(tempid[6])
a.showidlaber8.setText(tempid[7])
a.showidlaber9.setText(tempid[8])
a.showidlaber10.setText(tempid[9])
a.showsingerlaber1.setText(tempsinger[0])
a.showsingerlaber2.setText(tempsinger[1])
a.showsingerlaber3.setText(tempsinger[2])
a.showsingerlaber4.setText(tempsinger[3])
a.showsingerlaber5.setText(tempsinger[4])
a.showsingerlaber6.setText(tempsinger[5])
a.showsingerlaber7.setText(tempsinger[6])
a.showsingerlaber8.setText(tempsinger[7])
a.showsingerlaber9.setText(tempsinger[8])
a.showsingerlaber10.setText(tempsinger[9])
a.showtimelaber1.setText(tempsongtime[0])
a.showtimelaber2.setText(tempsongtime[1])
a.showtimelaber3.setText(tempsongtime[2])
a.showtimelaber4.setText(tempsongtime[3])
a.showtimelaber5.setText(tempsongtime[4])
a.showtimelaber6.setText(tempsongtime[5])
a.showtimelaber7.setText(tempsongtime[6])
a.showtimelaber8.setText(tempsongtime[7])
a.showtimelaber9.setText(tempsongtime[8])
a.showtimelaber10.setText(tempsongtime[9])
实例化b后运行一下程序看看
都是程序依照列表内容一个一个填上去的,只要我让每次更新label时候同时更新temp列表并读取id列表中的数据即可获得需要的id.
self.playmusicbutton1.clicked.connect(partial(self.playselectmusic,0))
self.playmusicbutton2.clicked.connect(partial(self.playselectmusic,1))
self.playmusicbutton3.clicked.connect(partial(self.playselectmusic,2))
self.playmusicbutton4.clicked.connect(partial(self.playselectmusic,3))
self.playmusicbutton5.clicked.connect(partial(self.playselectmusic,4))
self.playmusicbutton6.clicked.connect(partial(self.playselectmusic,5))
self.playmusicbutton7.clicked.connect(partial(self.playselectmusic,6))
self.playmusicbutton8.clicked.connect(partial(self.playselectmusic,7))
self.playmusicbutton9.clicked.connect(partial(self.playselectmusic,8))
self.playmusicbutton10.clicked.connect(partial(self.playselectmusic,9))
# 播放音乐
def playselectmusic(self, n):
print('Button {0} 被按下了'.format(n))
# self.progressBar= QProgressBar(self)
self.timer= QBasicTimer()
self.step = 0
global smillisecond
smillisecond=0
try:
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get('https://music.163.com/song?id='+str(tempid[n]),headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('main{}.jpg'.format(1),'wb') as f:
f.write(content)
img_path = Image.open('main{}.jpg'.format(1))
img_size = img_path.resize((150, 150))
img_size.save('main{}.jpg'.format(1),'png')
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon("main1.jpg"))
self.mainsongname.setText(tempname[n])
self.mainsinger.setText(tempsinger[n])
self.endtime.setText(tempsongtime[n])
m, s = tempsongtime[n].strip().split(':')
smillisecond=int(m)*60 + int(s)
print(smillisecond)
down_url='https://music.163.com/song/media/outer/url?id='+str(tempid[n])
print(down_url)
music=requests.get(url=down_url,headers=head).content
with open(tempname[n]+'.mp3','wb')as f:
f.write(music)
sz = os.path.getsize(tempname[n]+'.mp3')
if sz<=int(100000):
print('vip歌曲,下载失败')
c.show()
return
# pygame.init()
# sound = pygame.mixer.Sound(str(tempname[n]+'.mp3'))
# sound.set_volume(1)
# sound.play()
file = QUrl.fromLocalFile(str(tempname[n]+'.mp3')) # 音频文件路径
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
self.player.play()
time.sleep(2)
self.progressBar.setRange(0,smillisecond)
self.timer.start(1000,self)
except:
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get('https://music.163.com/song?id='+str(hotsongid[n]),headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('main{}.jpg'.format(1),'wb') as f:
f.write(content)
img_path = Image.open('main{}.jpg'.format(1))
img_size = img_path.resize((150, 150))
img_size.save('main{}.jpg'.format(1),'png')
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon("main1.jpg"))
self.mainsongname.setText(hotsongname[n])
self.mainsinger.setText(hotsinger[n])
self.endtime.setText(hotsongtime[n])
m, s = hotsongtime[n].strip().split(':')
smillisecond=int(m)*60 + int(s)
print(smillisecond)
self.progressBar.setRange(0, smillisecond)
down_url='https://music.163.com/song/media/outer/url?id='+str(hotsongid[n])
print(down_url)
music=requests.get(url=down_url,headers=head).content
with open(hotsongname[n]+'.mp3','wb')as f:
f.write(music)
sz = os.path.getsize(hotsongname[n]+'.mp3')
if sz<=int(100000):
print('vip歌曲,下载失败')
c.show()
return
# pygame.init()
# sound = pygame.mixer.Sound(str(hotsongname[n]+'.mp3'))
# sound.set_volume(1)
self.timer.start(1000,self)
# sound.play()
file = QUrl.fromLocalFile(str(hotsongname[n]+'.mp3'))
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
time.sleep(2)
self.player.play()
def timerEvent(self, event):
if self.step>=smillisecond:
self.timer.stop()
return
self.step+=1
self.progressBar.setValue(self.step)
记得当时再网上看教程一脸懵逼,这个函数没有被调用是怎么循环运行的,后来才想明白,我这一步是重新写了timerEvent函数,这个函数在计时器发射脉冲时被触发,原来是被隐藏的,所以获取time列表中的时间,格式是00:00这样的,所以要进行转换,获得以秒为单位的时间,所以可以理解为实例化一个计时器,每隔1000ms也就是一秒发射一个信号执行重写后的timerEvent函数,如果数字比时间大则停止计时.
def timerEvent(self, event):
if self.step>=smillisecond:
self.timer.stop()
return
self.step+=1
self.progressBar.setValue(self.step)
# 搜索功能
def searchmusic(self):
global tempname
global tempid
global tempsinger
global tempsongtime
tempname=[]
tempid=[]
tempsinger=[]
tempsongtime=[]
jihe=[]
t1=[0,4,8,12,16,20,24,28,32,36,40]
t2=[1,5,9,13,17,21,25,29,33,37,41]
t3=[3,7,11,15,19,23,27,31,35,39]
from selenium import webdriver
import unicodedata
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/#/search/m/?id=3778678&s='+self.inputsongname.text())
print(self.inputsongname.text())
wd.switch_to.frame(0)
time1=wd.find_elements_by_xpath('//div/div[contains(@class,"td")]')
for i in time1:
jihe.append(i.get_attribute('textContent'))
print(jihe)
for i in range(0,40):
if jihe[i]=='':
jihe.pop(i)
elements =wd.find_elements_by_xpath('//a[contains(@href,"/song?id")]')
for i in elements:
tempid.append(i.get_attribute('href')[30:])
for i in t1:
tempname.append(jihe[i])
for i in t2:
tempsinger.append(jihe[i])
for i in t3:
tempsongtime.append(jihe[i])
wd.quit()
a.showsongnamelaber1.setText(tempname[0])
a.showsongnamelaber2.setText(tempname[1])
a.showsongnamelaber3.setText(tempname[2])
a.showsongnamelaber4.setText(tempname[3])
a.showsongnamelaber5.setText(tempname[4])
a.showsongnamelaber6.setText(tempname[5])
a.showsongnamelaber7.setText(tempname[6])
a.showsongnamelaber8.setText(tempname[7])
a.showsongnamelaber9.setText(tempname[8])
a.showsongnamelaber10.setText(tempname[9])
a.showidlaber1.setText(tempid[0])
a.showidlaber2.setText(tempid[1])
a.showidlaber3.setText(tempid[2])
a.showidlaber4.setText(tempid[3])
a.showidlaber5.setText(tempid[4])
a.showidlaber6.setText(tempid[5])
a.showidlaber7.setText(tempid[6])
a.showidlaber8.setText(tempid[7])
a.showidlaber9.setText(tempid[8])
a.showidlaber10.setText(tempid[9])
a.showidlaber10.setText(tempid[9])
a.showidlaber10.setText(tempid[9])
a.showsingerlaber1.setText(tempsinger[0])
a.showsingerlaber2.setText(tempsinger[1])
a.showsingerlaber3.setText(tempsinger[2])
a.showsingerlaber4.setText(tempsinger[3])
a.showsingerlaber5.setText(tempsinger[4])
a.showsingerlaber6.setText(tempsinger[5])
a.showsingerlaber7.setText(tempsinger[6])
a.showsingerlaber8.setText(tempsinger[7])
a.showsingerlaber9.setText(tempsinger[8])
a.showsingerlaber10.setText(tempsinger[9])
a.showtimelaber1.setText(tempsongtime[0])
a.showtimelaber2.setText(tempsongtime[1])
a.showtimelaber3.setText(tempsongtime[2])
a.showtimelaber4.setText(tempsongtime[3])
a.showtimelaber5.setText(tempsongtime[4])
a.showtimelaber6.setText(tempsongtime[5])
a.showtimelaber7.setText(tempsongtime[6])
a.showtimelaber8.setText(tempsongtime[7])
a.showtimelaber9.setText(tempsongtime[8])
a.showtimelaber10.setText(tempsongtime[9])
实现搜索和简单,'https://music.163.com/#/search/m/?id=3778678&s='+需要搜索的关键字就可以访问
def stopplay(self):
global count
if count % 2 == 1:
# self.player.play()
print('开始播放')
self.player.play()
self.timer.start(1000,self)
else:
self.player.pause()
self.timer.stop()
# self.player.stop()
print('音乐暂停')
count=count+1
#
list = os.listdir()
localmusicname=[]
for s in list:
if 'mp3' in s:
localmusicname.append(s)
print(localmusicname)
localmusicname.extend(' '*10)
mark=0
#
def localmusic(self):
global mark
mark=1
self.showsongnamelaber1.setText(localmusicname[0])
self.showsongnamelaber2.setText(localmusicname[1])
self.showsongnamelaber3.setText(localmusicname[2])
self.showsongnamelaber4.setText(localmusicname[3])
self.showsongnamelaber5.setText(localmusicname[4])
self.showsongnamelaber6.setText(localmusicname[5])
self.showsongnamelaber7.setText(localmusicname[6])
self.showsongnamelaber8.setText(localmusicname[7])
self.showsongnamelaber9.setText(localmusicname[8])
self.showsongnamelaber10.setText(localmusicname[9])
self.showidlaber1.setText('')
self.showidlaber2.setText('')
self.showidlaber3.setText('')
self.showidlaber4.setText('')
self.showidlaber5.setText('')
self.showidlaber6.setText('')
self.showidlaber7.setText('')
self.showidlaber8.setText('')
self.showidlaber9.setText('')
self.showidlaber10.setText('')
self.showsingerlaber1.setText('')
self.showsingerlaber2.setText('')
self.showsingerlaber3.setText('')
self.showsingerlaber4.setText('')
self.showsingerlaber5.setText('')
self.showsingerlaber6.setText('')
self.showsingerlaber7.setText('')
self.showsingerlaber8.setText('')
self.showsingerlaber9.setText('')
self.showsingerlaber10.setText('')
self.showtimelaber1.setText('')
self.showtimelaber2.setText('')
self.showtimelaber3.setText('')
self.showtimelaber4.setText('')
self.showtimelaber5.setText('')
self.showtimelaber6.setText('')
self.showtimelaber7.setText('')
self.showtimelaber8.setText('')
self.showtimelaber9.setText('')
self.showtimelaber10.setText('')
def playselectmusic(self, n):
print('Button {0} 被按下了'.format(n))
# self.progressBar= QProgressBar(self)
self.timer= QBasicTimer()
self.step = 0
global smillisecond
smillisecond=0
if mark==0:
try:
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get('https://music.163.com/song?id='+str(tempid[n]),headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('main{}.jpg'.format(1),'wb') as f:
f.write(content)
img_path = Image.open('main{}.jpg'.format(1))
img_size = img_path.resize((150, 150))
img_size.save('main{}.jpg'.format(1),'png')
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon("main1.jpg"))
self.mainsongname.setText(tempname[n])
self.mainsinger.setText(tempsinger[n])
self.endtime.setText(tempsongtime[n])
m, s = tempsongtime[n].strip().split(':')
smillisecond=int(m)*60 + int(s)
print(smillisecond)
down_url='https://music.163.com/song/media/outer/url?id='+str(tempid[n])
print(down_url)
music=requests.get(url=down_url,headers=head).content
with open(tempname[n]+'.mp3','wb')as f:
f.write(music)
sz = os.path.getsize(tempname[n]+'.mp3')
if sz<=int(100000):
print('vip歌曲,下载失败')
os.remove(tempname[n]+'.mp3')
c.show()
return
# pygame.init()
# sound = pygame.mixer.Sound(str(tempname[n]+'.mp3'))
# sound.set_volume(1)
# sound.play()
file = QUrl.fromLocalFile(str(tempname[n]+'.mp3'))
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
self.player.play()
time.sleep(2)
self.progressBar.setRange(0,smillisecond)
self.timer.start(1000,self)
except:
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get('https://music.163.com/song?id='+str(hotsongid[n]),headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('main{}.jpg'.format(1),'wb') as f:
f.write(content)
img_path = Image.open('main{}.jpg'.format(1))
img_size = img_path.resize((150, 150))
img_size.save('main{}.jpg'.format(1),'png')
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon("main1.jpg"))
self.mainsongname.setText(hotsongname[n])
self.mainsinger.setText(hotsinger[n])
self.endtime.setText(hotsongtime[n])
m, s = hotsongtime[n].strip().split(':')
smillisecond=int(m)*60 + int(s)
print(smillisecond)
self.progressBar.setRange(0, smillisecond)
down_url='https://music.163.com/song/media/outer/url?id='+str(hotsongid[n])
print(down_url)
music=requests.get(url=down_url,headers=head).content
with open(hotsongname[n]+'.mp3','wb')as f:
f.write(music)
sz = os.path.getsize(hotsongname[n]+'.mp3')
if sz<=int(100000):
print('vip歌曲,下载失败')
os.remove(hotsongname[n]+'.mp3')
c.show()
return
# pygame.init()
# sound = pygame.mixer.Sound(str(hotsongname[n]+'.mp3'))
# sound.set_volume(1)
self.timer.start(1000,self)
# sound.play()
file = QUrl.fromLocalFile(str(hotsongname[n]+'.mp3'))
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
time.sleep(2)
self.player.play()
else:
file = QUrl.fromLocalFile(localmusicname[n])
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
self.player.play()
time.sleep(2)
def gethotlist(self):
temp=1
url='https://music.163.com/discover/playlist'
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
hotlistname=[]
hotlistpic=[]
hotlistid=[]
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
hotlistname.extend(html.xpath('//a[contains(@href,"playlist?") and contains(@class,"msk")]/@title')[0:4])
hotlistid.extend(html.xpath('//a[contains(@href,"playlist?") and contains(@class,"msk")]/@href')[0:4])
print(hotlistname)
print(hotlistid)
for i in hotlistid:
url='https://music.163.com'+i
print(url)
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('listpicture{}.jpg'.format(temp),'wb') as f:
f.write(content)
img_path = Image.open('listpicture{}.jpg'.format(temp))
img_size = img_path.resize((150, 150))
img_size.save('listpicture{}.jpg'.format(temp),'png')
temp=temp+1
self.label.setText('热门歌单')
self.picturename1.setText(hotlistname[0])
self.picturename2.setText(hotlistname[1])
self.picturename3.setText(hotlistname[2])
self.picturename4.setText(hotlistname[3])
self.picture1toolbutton1.setIconSize(QSize(150, 150))
self.picture1toolbutton1.setIcon(QIcon("listpicture1.jpg"))
self.stoptoolbutton.setIconSize(QSize(50, 50))
self.picture1toolbutton2.setIconSize(QSize(150, 150))
self.picture1toolbutton2.setIcon(QIcon("listpicture2.jpg"))
self.picture1toolbutton3.setIconSize(QSize(150, 150))
self.picture1toolbutton3.setIcon(QIcon("listpicture3.jpg"))
self.picture1toolbutton4.setIconSize(QSize(150, 150))
self.picture1toolbutton4.setIcon(QIcon("listpicture4.jpg"))
self.picture1toolbutton1.setToolTip(hotlistname[0])
self.picture1toolbutton2.setToolTip(hotlistname[1])
self.picture1toolbutton3.setToolTip(hotlistname[2])
self.picture1toolbutton4.setToolTip(hotlistname[3])
def playselectmusic(self, n):
print('Button {0} 被按下了'.format(n))
self.timer= QBasicTimer()
self.step = 0
global smillisecond
smillisecond=0
if mark==0:
try:
m, s = tempsongtime[n].strip().split(':')
smillisecond=int(m)*60 + int(s)
print(smillisecond)
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get('https://music.163.com/song?id='+str(tempid[n]),headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('{}.jpg'.format(tempname[n]+str(smillisecond)),'wb') as f:
f.write(content)
img_path = Image.open('{}.jpg'.format(tempname[n]+str(smillisecond)))
img_size = img_path.resize((150, 150))
img_size.save('{}.jpg'.format(tempname[n]+str(smillisecond)),'png')
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon('{}.jpg'.format(tempname[n]+str(smillisecond))))
self.mainsongname.setText(tempname[n])
self.mainsinger.setText(tempsinger[n])
self.endtime.setText(tempsongtime[n])
down_url='https://music.163.com/song/media/outer/url?id='+str(tempid[n])
print(down_url)
music=requests.get(url=down_url,headers=head).content
with open(tempname[n]+str(smillisecond)+'.mp3','wb')as f:
f.write(music)
sz = os.path.getsize(tempname[n]+str(smillisecond)+'.mp3')
if sz<=int(100000):
print('vip歌曲,下载失败')
os.remove(tempname[n]+str(smillisecond)+'.mp3')
c.show()
return
file = QUrl.fromLocalFile(str(tempname[n]+str(smillisecond)+'.mp3'))
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
self.player.play()
time.sleep(2)
self.progressBar.setRange(0,smillisecond)
self.timer.start(1000,self)
print('现在进入临时列表')
except:
print('现在进入热榜列表')
m, s = hotsongtime[n].strip().split(':')
smillisecond=int(m)*60 + int(s)
print(smillisecond)
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get('https://music.163.com/song?id='+str(hotsongid[n]),headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('{}.jpg'.format(hotsongname[n]+str(smillisecond)),'wb') as f:
f.write(content)
img_path = Image.open('{}.jpg'.format(hotsongname[n]+str(smillisecond)))
img_size = img_path.resize((150, 150))
img_size.save('{}.jpg'.format(hotsongname[n]+str(smillisecond)),'png')
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon('{}.jpg'.format(hotsongname[n]+str(smillisecond))))
self.mainsongname.setText(hotsongname[n])
self.mainsinger.setText(hotsinger[n])
self.endtime.setText(hotsongtime[n])
self.progressBar.setRange(0, smillisecond)
down_url='https://music.163.com/song/media/outer/url?id='+str(hotsongid[n])
print(down_url)
music=requests.get(url=down_url,headers=head).content
with open(hotsongname[n]+str(smillisecond)+'.mp3','wb')as f:
f.write(music)
sz = os.path.getsize(hotsongname[n]+str(smillisecond)+'.mp3')
if sz<=int(100000):
print('vip歌曲,下载失败')
os.remove(hotsongname[n]+str(smillisecond)+'.mp3')
c.show()
return
self.timer.start(1000,self)
file = QUrl.fromLocalFile(str(hotsongname[n]+str(smillisecond)+'.mp3'))
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
time.sleep(2)
self.player.play()
else:
smillisecond=int(localmusicname[n][-3:])
print('现在播放本地音乐')
def timerEvent(self,event):
if self.step>=smillisecond:
self.timer.stop()
return
self.step+=1
self.progressBar.setValue(self.step)
self.timer= QBasicTimer()
self.step = 0
self.progressBar.setRange(0,smillisecond)
self.timer.start(1000,self)
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon('{}.jpg'.format(localmusicname[n])))
self.endtime.setText(localmusicname[n][-3:])
print('输出结果'+localmusicname[n][-3:])
file = QUrl.fromLocalFile(localmusicname[n]+'.mp3')
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
self.player.play()
time.sleep(2)
def showsonglyrics(self):
d.show()
try:
url='http://music.163.com/api/song/media?id='+str(templyrics)
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone = requests.get(url=url, headers=head).text
songlyrics = json.loads(respone)
print(songlyrics['lyric'])
d.textEdit.setText(songlyrics['lyric'])
except:
d.textEdit.setText('当前未播放歌曲或正在播放本地音乐,无法显示歌词')
先写到这,完整代码如下 8.28,应该要完结了,如果要增加功能应该是就是上下首歌的切换,以及打包py文件了.作为一个假期作业已经写得够多了,以后去学c语言玩单片机去了
import requests
from lxml import etree
import random
import sys
import unicodedata
from PIL import Image
from PyQt5.QtWidgets import QApplication,QMainWindow,QFileDialog, QProgressBar,QStyleFactory
from PyQt5.Qt import QIcon,QSize
from PyQt5.QtCore import Qt, QBasicTimer
from PyQt5.Qt import *
from functools import partial
from PyQt5 import QtCore, QtWidgets
from selenium import webdriver
import time
from PyQt5.QtCore import QTimer
from Ui_作业主程序 import Ui_Form as A_Ui
from Ui_getlisturl import Ui_Form as B_Ui
from Ui_error import Ui_Form as C_Ui
from Ui_lyrics import Ui_Form as D_Ui
import os
from PyQt5 import QtMultimedia
from PyQt5.QtCore import QUrl
import json
randomsongname=[]
randomsongid=[]
count=0
mark=0
# 永久保存
hotsongname=[]
hotsongid=[]
hotsinger=[]
hotsongtime=[]
hotlistid=[]
#
# 临时保存
tempname=[]
tempid=[]
tempsinger=[]
tempsongtime=[]
#
# 获取热榜歌名,歌手,id,时长.
url='https://music.163.com/discover/toplist?id=3778678'
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=html.xpath('//a[contains(@href,"song?")]')
id_list=id_list[0:-11]
for id in id_list:
href=id.xpath('./@href')[0]
song_id=href.split('=')[1]
hotsongid.append(song_id)
song_name=id.xpath('./text()')[0]
hotsongname.append(song_name)
songandsinger=dict(zip(hotsongname,hotsongid))
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/discover/toplist?id=3778678')
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//div[contains(@class,"text")]')
time1=wd.find_elements_by_xpath('//td[contains(@class," s-fc3")]')
for i in elements:
hotsinger.append(i.get_attribute('title'))
for i in time1:
hotsongtime.append(i.get_attribute('textContent')[:-2])
wd.quit()
#
print(hotsongname[0])
# 随机获取四首歌并获取封面.
for i in songandsinger.keys():
randomsongname.append(i)
randomsongname= random.sample(randomsongname, 4)
print(randomsongname)
for j in randomsongname:
randomsongid.append('https://music.163.com/song?id='+songandsinger[j])
for i in songandsinger.keys():
hotsongname.append(i)
for i in songandsinger.values():
hotsongid.append(i)
def getrandompicture(randomsongid):
temp=1
for k in randomsongid:
url=k
print(url)
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('picture{}.jpg'.format(temp),'wb') as f:
f.write(content)
img_path = Image.open('picture{}.jpg'.format(temp))
img_size = img_path.resize((150, 150))
img_size.save('picture{}.jpg'.format(temp),'png')
temp=temp+1
getrandompicture(randomsongid)
#
# #
# list = os.listdir()
# localmusicname=[]
# for s in list:
# if 'mp3' in s:
# localmusicname.append(os.path.splitext(s)[0])
# print(localmusicname)
# localmusicname.extend(' '*10)
# #
#
class AUi(QtWidgets.QMainWindow, A_Ui):
def __init__(self):
super(AUi, self).__init__()
self.setupUi(self)
self.player = QtMultimedia.QMediaPlayer()
self.player.setVolume(int(50.0))
self.picturename1.setText(randomsongname[0])
self.picturename2.setText(randomsongname[1])
self.picturename3.setText(randomsongname[2])
self.picturename4.setText(randomsongname[3])
self.gethotmusicbutton.clicked.connect(self.restore)
self.progressBar.setTextVisible(False)
self.progressBar.setValue(0)
self.stoptoolbutton.clicked.connect(self.stopplay)
# self.pushButton.setFlat(True)
self.gethotlistbutton.clicked.connect(self.gethotlist)
self.getlocalmusicpushbutton.clicked.connect(self.localmusic)
self.inputsongnamebutton.clicked.connect(self.searchmusic)
self.parsesonglistbutton.clicked.connect(lambda:{
b.show()})
self.pushButton.clicked.connect(self.showsonglyrics)
self.picture1toolbutton1.clicked.connect(partial(self.parselist1,0))
self.picture1toolbutton2.clicked.connect(partial(self.parselist1,1))
self.picture1toolbutton3.clicked.connect(partial(self.parselist1,2))
self.picture1toolbutton4.clicked.connect(partial(self.parselist1,3))
self.playmusicbutton1.clicked.connect(partial(self.playselectmusic,0))
self.playmusicbutton2.clicked.connect(partial(self.playselectmusic,1))
self.playmusicbutton3.clicked.connect(partial(self.playselectmusic,2))
self.playmusicbutton4.clicked.connect(partial(self.playselectmusic,3))
self.playmusicbutton5.clicked.connect(partial(self.playselectmusic,4))
self.playmusicbutton6.clicked.connect(partial(self.playselectmusic,5))
self.playmusicbutton7.clicked.connect(partial(self.playselectmusic,6))
self.playmusicbutton8.clicked.connect(partial(self.playselectmusic,7))
self.playmusicbutton9.clicked.connect(partial(self.playselectmusic,8))
self.playmusicbutton10.clicked.connect(partial(self.playselectmusic,9))
self.picture1toolbutton1.setIconSize(QSize(150, 150))
self.picture1toolbutton1.setIcon(QIcon("picture1.jpg"))
self.stoptoolbutton.setIconSize(QSize(50, 50))
self.stoptoolbutton.setIcon(QIcon("暂停.jpg"))
self.picture1toolbutton2.setIconSize(QSize(150, 150))
self.picture1toolbutton2.setIcon(QIcon("picture2.jpg"))
self.picture1toolbutton3.setIconSize(QSize(150, 150))
self.picture1toolbutton3.setIcon(QIcon("picture3.jpg"))
self.picture1toolbutton4.setIconSize(QSize(150, 150))
self.picture1toolbutton4.setIcon(QIcon("picture4.jpg"))
self.picture1toolbutton1.setToolTip(randomsongname[0])
self.picture1toolbutton2.setToolTip(randomsongname[1])
self.picture1toolbutton3.setToolTip(randomsongname[2])
self.picture1toolbutton4.setToolTip(randomsongname[3])
self.showidlaber1.setText(hotsongid[0])
self.showidlaber2.setText(hotsongid[1])
self.showidlaber3.setText(hotsongid[2])
self.showidlaber4.setText(hotsongid[3])
self.showidlaber5.setText(hotsongid[4])
self.showidlaber6.setText(hotsongid[5])
self.showidlaber7.setText(hotsongid[6])
self.showidlaber8.setText(hotsongid[7])
self.showidlaber9.setText(hotsongid[8])
self.showidlaber10.setText(hotsongid[9])
self.showsongnamelaber1.setText(hotsongname[0])
self.showsongnamelaber2.setText(hotsongname[1])
self.showsongnamelaber3.setText(hotsongname[2])
self.showsongnamelaber4.setText(hotsongname[3])
self.showsongnamelaber5.setText(hotsongname[4])
self.showsongnamelaber6.setText(hotsongname[5])
self.showsongnamelaber7.setText(hotsongname[6])
self.showsongnamelaber8.setText(hotsongname[7])
self.showsongnamelaber9.setText(hotsongname[8])
self.showsongnamelaber10.setText(hotsongname[9])
self.showsingerlaber1.setText(hotsinger[0])
self.showsingerlaber2.setText(hotsinger[1])
self.showsingerlaber3.setText(hotsinger[2])
self.showsingerlaber4.setText(hotsinger[3])
self.showsingerlaber5.setText(hotsinger[4])
self.showsingerlaber6.setText(hotsinger[5])
self.showsingerlaber7.setText(hotsinger[6])
self.showsingerlaber8.setText(hotsinger[7])
self.showsingerlaber9.setText(hotsinger[8])
self.showsingerlaber10.setText(hotsinger[9])
self.showtimelaber1.setText(hotsongtime[0])
self.showtimelaber2.setText(hotsongtime[1])
self.showtimelaber3.setText(hotsongtime[2])
self.showtimelaber4.setText(hotsongtime[3])
self.showtimelaber5.setText(hotsongtime[4])
self.showtimelaber6.setText(hotsongtime[5])
self.showtimelaber7.setText(hotsongtime[6])
self.showtimelaber8.setText(hotsongtime[7])
self.showtimelaber9.setText(hotsongtime[8])
self.showtimelaber10.setText(hotsongtime[9])
self.setWindowOpacity(0.95)
def showsonglyrics(self):
d.show()
try:
url='http://music.163.com/api/song/media?id='+str(templyrics)
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone = requests.get(url=url, headers=head).text
songlyrics = json.loads(respone)
print(songlyrics['lyric'])
d.textEdit.setText(songlyrics['lyric'])
except:
d.textEdit.setText('当前未播放歌曲或正在播放本地音乐,无法显示歌词')
def localmusic(self):
global mark
global localmusicname
mark=1
list = os.listdir()
localmusicname=[]
for s in list:
if 'mp3' in s:
localmusicname.append(os.path.splitext(s)[0])
print(localmusicname)
localmusicname.extend(' '*10)
self.showsongnamelaber1.setText(localmusicname[0][:-3])
self.showsongnamelaber2.setText(localmusicname[1][:-3])
self.showsongnamelaber3.setText(localmusicname[2][:-3])
self.showsongnamelaber4.setText(localmusicname[3][:-3])
self.showsongnamelaber5.setText(localmusicname[4][:-3])
self.showsongnamelaber6.setText(localmusicname[5][:-3])
self.showsongnamelaber7.setText(localmusicname[6][:-3])
self.showsongnamelaber8.setText(localmusicname[7][:-3])
self.showsongnamelaber9.setText(localmusicname[8][:-3])
self.showsongnamelaber10.setText(localmusicname[9][:-3])
self.showidlaber1.setText('')
self.showidlaber2.setText('')
self.showidlaber3.setText('')
self.showidlaber4.setText('')
self.showidlaber5.setText('')
self.showidlaber6.setText('')
self.showidlaber7.setText('')
self.showidlaber8.setText('')
self.showidlaber9.setText('')
self.showidlaber10.setText('')
self.showsingerlaber1.setText('')
self.showsingerlaber2.setText('')
self.showsingerlaber3.setText('')
self.showsingerlaber4.setText('')
self.showsingerlaber5.setText('')
self.showsingerlaber6.setText('')
self.showsingerlaber7.setText('')
self.showsingerlaber8.setText('')
self.showsingerlaber9.setText('')
self.showsingerlaber10.setText('')
self.showtimelaber1.setText(localmusicname[0][-3:])
self.showtimelaber2.setText(localmusicname[1][-3:])
self.showtimelaber3.setText(localmusicname[2][-3:])
self.showtimelaber4.setText(localmusicname[3][-3:])
self.showtimelaber5.setText(localmusicname[4][-3:])
self.showtimelaber6.setText(localmusicname[5][-3:])
self.showtimelaber7.setText(localmusicname[6][-3:])
self.showtimelaber8.setText(localmusicname[7][-3:])
self.showtimelaber9.setText(localmusicname[8][-3:])
self.showtimelaber10.setText(localmusicname[9][-3:])
def stopplay(self):
global count
if count % 2 == 1:
# self.player.play()
print('开始播放')
self.player.play()
self.timer.start(1000,self)
else:
self.player.pause()
self.timer.stop()
# self.player.stop()
print('音乐暂停')
count=count+1
# 播放音乐
def playselectmusic(self, n):
print('Button {0} 被按下了'.format(n))
self.timer= QBasicTimer()
self.step = 0
global smillisecond
global templyrics
templyrics=0
smillisecond=0
if mark==0:
try:
m, s = tempsongtime[n].strip().split(':')
smillisecond=int(m)*60 + int(s)
print(smillisecond)
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get('https://music.163.com/song?id='+str(tempid[n]),headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('{}.jpg'.format(tempname[n]+str(smillisecond)),'wb') as f:
f.write(content)
img_path = Image.open('{}.jpg'.format(tempname[n]+str(smillisecond)))
img_size = img_path.resize((150, 150))
img_size.save('{}.jpg'.format(tempname[n]+str(smillisecond)),'png')
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon('{}.jpg'.format(tempname[n]+str(smillisecond))))
self.mainsongname.setText(tempname[n])
self.mainsinger.setText(tempsinger[n])
self.endtime.setText(tempsongtime[n])
down_url='https://music.163.com/song/media/outer/url?id='+str(tempid[n])
print(down_url)
music=requests.get(url=down_url,headers=head).content
with open(tempname[n]+str(smillisecond)+'.mp3','wb')as f:
f.write(music)
sz = os.path.getsize(tempname[n]+str(smillisecond)+'.mp3')
if sz<=int(100000):
print('vip歌曲,下载失败')
os.remove(tempname[n]+str(smillisecond)+'.mp3')
c.show()
return
file = QUrl.fromLocalFile(str(tempname[n]+str(smillisecond)+'.mp3'))
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
self.player.play()
time.sleep(2)
self.progressBar.setRange(0,smillisecond)
self.timer.start(1000,self)
print('现在进入临时列表')
templyrics=tempid[n]
except:
print('现在进入热榜列表')
m, s = hotsongtime[n].strip().split(':')
smillisecond=int(m)*60 + int(s)
print(smillisecond)
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get('https://music.163.com/song?id='+str(hotsongid[n]),headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('{}.jpg'.format(hotsongname[n]+str(smillisecond)),'wb') as f:
f.write(content)
img_path = Image.open('{}.jpg'.format(hotsongname[n]+str(smillisecond)))
img_size = img_path.resize((150, 150))
img_size.save('{}.jpg'.format(hotsongname[n]+str(smillisecond)),'png')
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon('{}.jpg'.format(hotsongname[n]+str(smillisecond))))
self.mainsongname.setText(hotsongname[n])
self.mainsinger.setText(hotsinger[n])
self.endtime.setText(hotsongtime[n])
self.progressBar.setRange(0, smillisecond)
down_url='https://music.163.com/song/media/outer/url?id='+str(hotsongid[n])
print(down_url)
music=requests.get(url=down_url,headers=head).content
with open(hotsongname[n]+str(smillisecond)+'.mp3','wb')as f:
f.write(music)
sz = os.path.getsize(hotsongname[n]+str(smillisecond)+'.mp3')
if sz<=int(100000):
print('vip歌曲,下载失败')
os.remove(hotsongname[n]+str(smillisecond)+'.mp3')
c.show()
return
self.timer.start(1000,self)
file = QUrl.fromLocalFile(str(hotsongname[n]+str(smillisecond)+'.mp3'))
content = QtMultimedia.QMediaContent(file)
templyrics=hotsongid[n]
self.player.setMedia(content)
time.sleep(2)
self.player.play()
else:
smillisecond=int(localmusicname[n][-3:])
print('现在播放本地音乐')
def timerEvent(self,event):
if self.step>=smillisecond:
self.timer.stop()
return
self.step+=1
self.progressBar.setValue(self.step)
self.timer= QBasicTimer()
self.step = 0
self.progressBar.setRange(0,smillisecond)
self.timer.start(1000,self)
self.mainpicture.setIconSize(QSize(150, 150))
self.mainpicture.setIcon(QIcon('{}.jpg'.format(localmusicname[n])))
self.endtime.setText(localmusicname[n][-3:])
print('输出结果'+localmusicname[n][-3:])
file = QUrl.fromLocalFile(localmusicname[n]+'.mp3')
content = QtMultimedia.QMediaContent(file)
self.player.setMedia(content)
self.player.play()
time.sleep(2)
def timerEvent(self, event):
if self.step>=smillisecond:
self.timer.stop()
return
self.step+=1
self.progressBar.setValue(self.step)
# 还原界面
def restore(self):
global mark
mark=0
global tempname
global tempid
global tempsinger
global tempsongtime
tempname=hotsongtime
tempid=hotsongid
tempsinger=hotsinger
tempsongtime=hotsongtime
self.showidlaber1.setText(hotsongid[0])
self.showidlaber2.setText(hotsongid[1])
self.showidlaber3.setText(hotsongid[2])
self.showidlaber4.setText(hotsongid[3])
self.showidlaber5.setText(hotsongid[4])
self.showidlaber6.setText(hotsongid[5])
self.showidlaber7.setText(hotsongid[6])
self.showidlaber8.setText(hotsongid[7])
self.showidlaber9.setText(hotsongid[8])
self.showidlaber10.setText(hotsongid[9])
self.showsongnamelaber1.setText(hotsongname[0])
self.showsongnamelaber2.setText(hotsongname[1])
self.showsongnamelaber3.setText(hotsongname[2])
self.showsongnamelaber4.setText(hotsongname[3])
self.showsongnamelaber5.setText(hotsongname[4])
self.showsongnamelaber6.setText(hotsongname[5])
self.showsongnamelaber7.setText(hotsongname[6])
self.showsongnamelaber8.setText(hotsongname[7])
self.showsongnamelaber9.setText(hotsongname[8])
self.showsongnamelaber10.setText(hotsongname[9])
self.showsingerlaber1.setText(hotsinger[0])
self.showsingerlaber2.setText(hotsinger[1])
self.showsingerlaber3.setText(hotsinger[2])
self.showsingerlaber4.setText(hotsinger[3])
self.showsingerlaber5.setText(hotsinger[4])
self.showsingerlaber6.setText(hotsinger[5])
self.showsingerlaber7.setText(hotsinger[6])
self.showsingerlaber8.setText(hotsinger[7])
self.showsingerlaber9.setText(hotsinger[8])
self.showsingerlaber10.setText(hotsinger[9])
self.showtimelaber1.setText(hotsongtime[0])
self.showtimelaber2.setText(hotsongtime[1])
self.showtimelaber3.setText(hotsongtime[2])
self.showtimelaber4.setText(hotsongtime[3])
self.showtimelaber5.setText(hotsongtime[4])
self.showtimelaber6.setText(hotsongtime[5])
self.showtimelaber7.setText(hotsongtime[6])
self.showtimelaber8.setText(hotsongtime[7])
self.showtimelaber9.setText(hotsongtime[8])
self.showtimelaber10.setText(hotsongtime[9])
self.picture1toolbutton1.setIconSize(QSize(150, 150))
self.picture1toolbutton1.setIcon(QIcon("picture1.jpg"))
self.picture1toolbutton2.setIconSize(QSize(150, 150))
self.picture1toolbutton2.setIcon(QIcon("picture2.jpg"))
self.picture1toolbutton3.setIconSize(QSize(150, 150))
self.picture1toolbutton3.setIcon(QIcon("picture3.jpg"))
self.picture1toolbutton4.setIconSize(QSize(150, 150))
self.picture1toolbutton4.setIcon(QIcon("picture4.jpg"))
self.picture1toolbutton1.setToolTip(randomsongname[0])
self.picture1toolbutton2.setToolTip(randomsongname[1])
self.picture1toolbutton3.setToolTip(randomsongname[2])
self.picture1toolbutton4.setToolTip(randomsongname[3])
# 搜索功能
def searchmusic(self):
global mark
mark=0
global tempname
global tempid
global tempsinger
global tempsongtime
tempname=[]
tempid=[]
tempsinger=[]
tempsongtime=[]
jihe=[]
t1=[0,4,8,12,16,20,24,28,32,36,40]
t2=[1,5,9,13,17,21,25,29,33,37,41]
t3=[3,7,11,15,19,23,27,31,35,39]
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/#/search/m/?id=3778678&s='+self.inputsongname.text())
print(self.inputsongname.text())
wd.switch_to.frame(0)
time1=wd.find_elements_by_xpath('//div/div[contains(@class,"td")]')
for i in time1:
jihe.append(i.get_attribute('textContent'))
print(jihe)
for i in range(0,40):
if jihe[i]=='':
jihe.pop(i)
elements =wd.find_elements_by_xpath('//a[contains(@href,"/song?id")]')
for i in elements:
tempid.append(i.get_attribute('href')[30:])
for i in t1:
tempname.append(jihe[i])
for i in t2:
tempsinger.append(jihe[i])
for i in t3:
tempsongtime.append(jihe[i])
wd.quit()
a.showsongnamelaber1.setText(tempname[0])
a.showsongnamelaber2.setText(tempname[1])
a.showsongnamelaber3.setText(tempname[2])
a.showsongnamelaber4.setText(tempname[3])
a.showsongnamelaber5.setText(tempname[4])
a.showsongnamelaber6.setText(tempname[5])
a.showsongnamelaber7.setText(tempname[6])
a.showsongnamelaber8.setText(tempname[7])
a.showsongnamelaber9.setText(tempname[8])
a.showsongnamelaber10.setText(tempname[9])
a.showidlaber1.setText(tempid[0])
a.showidlaber2.setText(tempid[1])
a.showidlaber3.setText(tempid[2])
a.showidlaber4.setText(tempid[3])
a.showidlaber5.setText(tempid[4])
a.showidlaber6.setText(tempid[5])
a.showidlaber7.setText(tempid[6])
a.showidlaber8.setText(tempid[7])
a.showidlaber9.setText(tempid[8])
a.showidlaber10.setText(tempid[9])
a.showidlaber10.setText(tempid[9])
a.showidlaber10.setText(tempid[9])
a.showsingerlaber1.setText(tempsinger[0])
a.showsingerlaber2.setText(tempsinger[1])
a.showsingerlaber3.setText(tempsinger[2])
a.showsingerlaber4.setText(tempsinger[3])
a.showsingerlaber5.setText(tempsinger[4])
a.showsingerlaber6.setText(tempsinger[5])
a.showsingerlaber7.setText(tempsinger[6])
a.showsingerlaber8.setText(tempsinger[7])
a.showsingerlaber9.setText(tempsinger[8])
a.showsingerlaber10.setText(tempsinger[9])
a.showtimelaber1.setText(tempsongtime[0])
a.showtimelaber2.setText(tempsongtime[1])
a.showtimelaber3.setText(tempsongtime[2])
a.showtimelaber4.setText(tempsongtime[3])
a.showtimelaber5.setText(tempsongtime[4])
a.showtimelaber6.setText(tempsongtime[5])
a.showtimelaber7.setText(tempsongtime[6])
a.showtimelaber8.setText(tempsongtime[7])
a.showtimelaber9.setText(tempsongtime[8])
a.showtimelaber10.setText(tempsongtime[9])
def gethotlist(self):
global mark
mark=0
global hotlistid
temp=1
url='https://music.163.com/discover/playlist'
head={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
hotlistname=[]
hotlistpic=[]
hotlistid=[]
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
hotlistname.extend(html.xpath('//a[contains(@href,"playlist?") and contains(@class,"msk")]/@title')[0:4])
hotlistid.extend(html.xpath('//a[contains(@href,"playlist?") and contains(@class,"msk")]/@href')[0:4])
print(hotlistname)
print(hotlistid)
for i in hotlistid:
url='https://music.163.com'+i
print(url)
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=str(html.xpath('//img/@data-src'))
id_list=id_list[2:-2]
print(id_list)
respone=requests.get(id_list,headers=head)
content = respone.content
with open('listpicture{}.jpg'.format(temp),'wb') as f:
f.write(content)
img_path = Image.open('listpicture{}.jpg'.format(temp))
img_size = img_path.resize((150, 150))
img_size.save('listpicture{}.jpg'.format(temp),'png')
temp=temp+1
self.label.setText('热门歌单')
self.picturename1.setText(hotlistname[0])
self.picturename2.setText(hotlistname[1])
self.picturename3.setText(hotlistname[2])
self.picturename4.setText(hotlistname[3])
self.picture1toolbutton1.setIconSize(QSize(150, 150))
self.picture1toolbutton1.setIcon(QIcon("listpicture1.jpg"))
self.stoptoolbutton.setIconSize(QSize(50, 50))
self.picture1toolbutton2.setIconSize(QSize(150, 150))
self.picture1toolbutton2.setIcon(QIcon("listpicture2.jpg"))
self.picture1toolbutton3.setIconSize(QSize(150, 150))
self.picture1toolbutton3.setIcon(QIcon("listpicture3.jpg"))
self.picture1toolbutton4.setIconSize(QSize(150, 150))
self.picture1toolbutton4.setIcon(QIcon("listpicture4.jpg"))
self.picture1toolbutton1.setToolTip(hotlistname[0])
self.picture1toolbutton2.setToolTip(hotlistname[1])
self.picture1toolbutton3.setToolTip(hotlistname[2])
self.picture1toolbutton4.setToolTip(hotlistname[3])
def parselist1(self,n):
global mark
mark=0
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/#'+hotlistid[n])
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//a[contains(@href,"/song?id")]')
global tempname
global tempid
global tempsinger
global tempsongtime
tempname=[]
tempid=[]
tempsinger=[]
tempsongtime=[]
for i in elements:
tempid.append(i.get_attribute('href')[30:])
elements2 =wd.find_elements_by_xpath('//b[@title]')
for i in elements2:
s=i.get_attribute('title')
temp=unicodedata.normalize('NFKC', str(s))
tempname.append(temp)
elements3 =wd.find_elements_by_xpath('//div[contains(@class,"text")]')
for i in elements3:
tempsinger.append(i.get_attribute('title'))
for i in range(1,11):
if tempsinger[i]=='':
tempsinger.pop(i)
time2=wd.find_elements_by_xpath('//td[contains(@class," s-fc3")]')
for i in time2:
tempsongtime.append(i.get_attribute('textContent')[:-2])
print(tempid)
print(tempname)
wd.quit()
b.close()
a.showsongnamelaber1.setText(tempname[0])
a.showsongnamelaber2.setText(tempname[1])
a.showsongnamelaber3.setText(tempname[2])
a.showsongnamelaber4.setText(tempname[3])
a.showsongnamelaber5.setText(tempname[4])
a.showsongnamelaber6.setText(tempname[5])
a.showsongnamelaber7.setText(tempname[6])
a.showsongnamelaber8.setText(tempname[7])
a.showsongnamelaber9.setText(tempname[8])
a.showsongnamelaber10.setText(tempname[9])
a.showidlaber1.setText(tempid[0])
a.showidlaber2.setText(tempid[1])
a.showidlaber3.setText(tempid[2])
a.showidlaber4.setText(tempid[3])
a.showidlaber5.setText(tempid[4])
a.showidlaber6.setText(tempid[5])
a.showidlaber7.setText(tempid[6])
a.showidlaber8.setText(tempid[7])
a.showidlaber9.setText(tempid[8])
a.showidlaber10.setText(tempid[9])
a.showsingerlaber1.setText(tempsinger[0])
a.showsingerlaber2.setText(tempsinger[1])
a.showsingerlaber3.setText(tempsinger[2])
a.showsingerlaber4.setText(tempsinger[3])
a.showsingerlaber5.setText(tempsinger[4])
a.showsingerlaber6.setText(tempsinger[5])
a.showsingerlaber7.setText(tempsinger[6])
a.showsingerlaber8.setText(tempsinger[7])
a.showsingerlaber9.setText(tempsinger[8])
a.showsingerlaber10.setText(tempsinger[9])
a.showtimelaber1.setText(tempsongtime[0])
a.showtimelaber2.setText(tempsongtime[1])
a.showtimelaber3.setText(tempsongtime[2])
a.showtimelaber4.setText(tempsongtime[3])
a.showtimelaber5.setText(tempsongtime[4])
a.showtimelaber6.setText(tempsongtime[5])
a.showtimelaber7.setText(tempsongtime[6])
a.showtimelaber8.setText(tempsongtime[7])
a.showtimelaber9.setText(tempsongtime[8])
a.showtimelaber10.setText(tempsongtime[9])
#
class BUi(QtWidgets.QMainWindow, B_Ui):
def __init__(self):
super(BUi, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.parselist)
# 解析歌单
def parselist(self):
global mark
mark=0
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get(self.geturllineedit.text())
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//a[contains(@href,"/song?id")]')
global tempname
global tempid
global tempsinger
global tempsongtime
tempname=[]
tempid=[]
tempsinger=[]
tempsongtime=[]
for i in elements:
tempid.append(i.get_attribute('href')[30:])
elements2 =wd.find_elements_by_xpath('//b[@title]')
for i in elements2:
s=i.get_attribute('title')
temp=unicodedata.normalize('NFKC', str(s))
tempname.append(temp)
elements3 =wd.find_elements_by_xpath('//div[contains(@class,"text")]')
for i in elements3:
tempsinger.append(i.get_attribute('title'))
for i in range(1,11):
if tempsinger[i]=='':
tempsinger.pop(i)
time2=wd.find_elements_by_xpath('//td[contains(@class," s-fc3")]')
for i in time2:
tempsongtime.append(i.get_attribute('textContent')[:-2])
print(tempid)
print(tempname)
wd.quit()
b.close()
a.showsongnamelaber1.setText(tempname[0])
a.showsongnamelaber2.setText(tempname[1])
a.showsongnamelaber3.setText(tempname[2])
a.showsongnamelaber4.setText(tempname[3])
a.showsongnamelaber5.setText(tempname[4])
a.showsongnamelaber6.setText(tempname[5])
a.showsongnamelaber7.setText(tempname[6])
a.showsongnamelaber8.setText(tempname[7])
a.showsongnamelaber9.setText(tempname[8])
a.showsongnamelaber10.setText(tempname[9])
a.showidlaber1.setText(tempid[0])
a.showidlaber2.setText(tempid[1])
a.showidlaber3.setText(tempid[2])
a.showidlaber4.setText(tempid[3])
a.showidlaber5.setText(tempid[4])
a.showidlaber6.setText(tempid[5])
a.showidlaber7.setText(tempid[6])
a.showidlaber8.setText(tempid[7])
a.showidlaber9.setText(tempid[8])
a.showidlaber10.setText(tempid[9])
a.showsingerlaber1.setText(tempsinger[0])
a.showsingerlaber2.setText(tempsinger[1])
a.showsingerlaber3.setText(tempsinger[2])
a.showsingerlaber4.setText(tempsinger[3])
a.showsingerlaber5.setText(tempsinger[4])
a.showsingerlaber6.setText(tempsinger[5])
a.showsingerlaber7.setText(tempsinger[6])
a.showsingerlaber8.setText(tempsinger[7])
a.showsingerlaber9.setText(tempsinger[8])
a.showsingerlaber10.setText(tempsinger[9])
a.showtimelaber1.setText(tempsongtime[0])
a.showtimelaber2.setText(tempsongtime[1])
a.showtimelaber3.setText(tempsongtime[2])
a.showtimelaber4.setText(tempsongtime[3])
a.showtimelaber5.setText(tempsongtime[4])
a.showtimelaber6.setText(tempsongtime[5])
a.showtimelaber7.setText(tempsongtime[6])
a.showtimelaber8.setText(tempsongtime[7])
a.showtimelaber9.setText(tempsongtime[8])
a.showtimelaber10.setText(tempsongtime[9])
#
class CUi(QtWidgets.QMainWindow, C_Ui):
def __init__(self):
super(CUi, self).__init__()
self.setupUi(self)
class DUi(QtWidgets.QMainWindow, D_Ui):
def __init__(self):
super(DUi, self).__init__()
self.setupUi(self)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setStyle(QStyleFactory.create("GTK+"))
a = AUi()
a.show()
b = BUi()
c = CUi()
d = DUi()
sys.exit(app.exec_())