Freshman primary school homework is to write a small program with pyqt5 and crawlers. It happens that there is a shortage of boring songs recently. I wonder if I can write a small software for climbing music Then send it in the form of articles from 0 to finished products qtdesigner will be used in UI design. Dragging the module can complete the layout, which is very simple The overall code is not difficult. You can understand a little about python Moreover, UI code and function code are separated to facilitate change
1, Clear functional objectives
Probably refer to the layout of Netease cloud
On the left is the function bar, on the right is the information, and on the top is the search function Then the function I want to achieve is to obtain the hot list, obtain the popular song list, give the song list link, analyze the song list, randomly recommend the hot list songs, and play local music
2, UI design
1. Select the main interface
Use no function bar window
So far, our interface is basically completed. The following describes how to realize the button function and the display of pictures
3, Function realization
1. Get the hot list
The hot song list url is: https://music.163.com/#/discover/toplist?id=3778678
By checking the elements, we can find the song name and song ID. note! If you don't delete / # in the url, you can't crawl. The real url is' https://music.163.com/discover/toplist?id=3778678 ’
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))
Now explain the code. There's nothing to say at the beginning. Get the web page source code through requests and parse it with xpath Take a look at the content of the review element. The song ID is placed in the href of the a tag, and b is included in the a tag. The text content can be obtained directly by using the text() method The reason for slicing the idlist is that some irrelevant contents need to be deleted And song_id=href.split('=') [1] means to split the string and get the pure digital ID after = and finally get a dictionary containing the name and ID of the hit song. As for why it is a dictionary, please explain later
2. Randomly get four hit songs and get the cover
First, let's talk about the general idea: get the keys in the dictionary and form a list, then randomly draw out four song names, then get the id with the song name, and access the spliced complete id. then find the song cover and compress the picture with 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]) # Get four songs at random and get the cover 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)
Explain the code,
Review the elements and look at the picture address. One is a thumbnail and the other is a high-definition large picture. Let's first select the large picture. I searched that there is only one containing data SRC, so the xpath can be written as' / / img / @ data SRC '. As for why the slice
See the difference?
One is[‘ http://p2.music.126.net/6EX0yj-r5GBLzSNU20rrmQ==/109951165791933542.jpg ’]
The other is http://p2.music.126.net/6EX0yj-r5GBLzSNU20rrmQ==/109951165791933542.jpg
If you do not directly import slices, an error will be reported
The data saving is divided into two parts. One is permanent saving (including the song name, id, duration, singer and popular song list id of hot list songs, which is convenient for subsequent display and interface reset.) The other is temporary saving, including the content of the song list and the content obtained by searching songs, which will be changed at any time
3. Initialization interface
The function of the code is to obtain the name, id, singer and duration of the hot list song And save it in the list Then select four random songs and save the cover for displayimport 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_Job Master Program 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 # Permanent preservation hotsongname=[] hotsongid=[] hotsinger=[] hotsongtime=[] hotlistid=[] # # Temporary storage tempname=[] tempid=[] tempsinger=[] tempsongtime=[] # # Get the title, singer, id and duration of the hit song 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]) # Get four songs at random and get the cover 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("suspend.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_())
Now that the framework is built, you can add meat
4. Analyze the song list
Analyzing the song list should be the most problematic place here As for the pictures of the storage addresses of various elements, I won't put them There's a strange problem herename=[] 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'))
The output is like this
But I wanted to put the elements in the list. Something magical happened
# 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)
See the difference? There is a lot of garbled code in the title of the song So far, the reason is unknown
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()
It was successfully solved under the guidance of the boss
The code part is solved. Now switch to the graphical interface
Design a new interface named getlisturl ui
First write a signal slot, which is used to transfer the text information entered by the user to the written analysis song list function when clicking the analysis button, and close itself
# class BUi(QtWidgets.QMainWindow, B_Ui): def __init__(self): super(BUi, self).__init__() self.setupUi(self) self.pushButton.clicked.connect(self.parselist) # Analyze song list 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])
Run the program after instantiating b
5. Play / download
Downloading songs is particularly easy It's prefix + song id. previously, we got the data including song name id, just fill it in I used a stupid method here. Behind the ui interface is actually a list
The program fills in one by one according to the contents of the list. As long as I update the temp list and read the data in the id list every time I update the label, I can get the required id
Manually write 10 signal slots, each transmitting its corresponding serial numberself.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))
Explain the code. This code completes three functions: obtaining the cover progress bar and playing music Let's start with downloading music The first step is to get the song cover. I said it in getting the cover, so I won't repeat it. Then I get the complete download url, and continue to connect and download After naming, use the os package to check its size. If it is less than 100000 B, it means that the vip song cannot be downloaded. Display the c window with error prompt and end the function If everything is normal, use the module in pyqt to read and play music If you download music when the hot song list is displayed at the beginning, all temp lists are empty, and an error is reported in the try statement, go to the execution of the exception statement. If you download music in the search or parsing interface, the content in the temp list can be downloaded normally, and the exception statement will not be executed In the restore function, assign the permanent list to the temporary list, and it can still run normally# Play music def playselectmusic(self, n): print('Button {0} Pressed'.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 song,Download failed') 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')) # Audio file path 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 song,Download failed') 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()
6. Progress bar
The progress bar is a big pit. It takes a lot of time here. First, the timer sends a signal every specific millisecond,I remember reading the tutorial on the Internet at that time. I was confused about how this function runs circularly without being called. Later, I figured out that this step is to rewrite the timerEvent function. This function is triggered when the timer emits pulses. It was originally hidden, so I get the time in the time list in the format of 00:00, so I need to convert it to obtain the time in seconds, Therefore, it can be understood as instantiating a timer, transmitting a signal every 1000ms, that is, one second, and executing the rewritten timerEvent function. If the number is larger than time, stop timingdef timerEvent(self, event): if self.step>=smillisecond: self.timer.stop() return self.step+=1 self.progressBar.setValue(self.step)
def timerEvent(self, event): if self.step>=smillisecond: self.timer.stop() return self.step+=1 self.progressBar.setValue(self.step)
7. Realize the search function
"Search and simple," https://music.163.com/#/search/m/?id=3778678&s= '+ the keywords to be searched can be accessed# Search function 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])
8. Pause and resume the song
def stopplay(self): global count if count % 2 == 1: # self.player.play() print('Start playing') self.player.play() self.timer.start(1000,self) else: self.player.pause() self.timer.stop() # self.player.stop() print('Music pause') count=count+1
Write this first. The complete code is as follows
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_Job Master Program 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 # Permanent preservation hotsongname=[] hotsongid=[] hotsinger=[] hotsongtime=[] hotlistid=[] # # Temporary storage tempname=[] tempid=[] tempsinger=[] tempsongtime=[] # # Get the title, singer, id and duration of the hit song 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]) # Get four songs at random and get the cover 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("suspend.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 stopplay(self): global count if count % 2 == 1: # self.player.play() print('Start playing') self.player.play() else: self.player.pause() # self.player.stop() print('Music pause') count=count+1 # Play music def playselectmusic(self, n): print('Button {0} Pressed'.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 song,Download failed') 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')) # Audio file path 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 song,Download failed') 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) # Restore interface def restore(self): 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]) # Search function 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]) def gethotlist(self): 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:10]) hotlistpic.extend(html.xpath('//img[contains(@src,"http://p2.music.126.net")]/@src')[0:4]) hotlistid.extend(html.xpath('//a[contains(@href,"playlist?") and contains(@class,"msk")]/@href')[0:10]) print(hotlistpic) for i in hotlistpic: respone=requests.get(i,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('Hit list') 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): 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) # Analyze song list 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]) # class CUi(QtWidgets.QMainWindow, C_Ui): def __init__(self): super(CUi, 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() sys.exit(app.exec_()) # print(tempname) # print(tempid) # print(tempsinger) # print(tempsongtime)