How to realize Free Music Downloader by Python
Today, I will talk to you about how to achieve a free music downloader in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Text 1) ideas
Enter any singer on a music home page, such as Li XX.
Where does the music come from? -in the server of the website
How do I get music from the website? -initiate a web request to the website
Finally, use tkinter to make an interface download box.
2) Environment
The environment used in this paper is as follows: Python3, Pycharm,requests, tkinter module.
Module installation: if you have any questions about the pip install + module name environment, you can come to me directly. (installation package, video, activation code are all available)
3) Code demonstration
3.1 Open Review element F12
Find the header file address:
Headers = {'Referer':' https://y.qq.com/portal/search.html', 'User-Agent':' Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)''Chrome/77.0.3865.90 Safari/537.36'}
3.2 complete project attached
#-*-coding: utf-8-*-from tkinter import * import requestsimport jsonimport os headers = {'Referer':' https://y.qq.com/portal/search.html', 'User-Agent':' Mozilla/5.0 (Windows NT 6.1; Win64 X64) AppleWebKit/537.36 (KHTML Like Gecko)''Chrome/77.0.3865.90 Safari/537.36'} def downlaod_song (): music_info_list = [] name = entry.get () page ='1' num = '10' url = f' https://c.y.qq.com/soso/fcgi-bin/client_search_cp?p={page}&n={num}&w={name}' response = requests.get (url). Text # splits the response into a dictionary similar to the json format, but it is still the string music_json = response [9:-1] music_data = json.loads (music_json) music_list = music_data ['data'] [' song'] ['list'] for music in music_list: music_name = music [' songname'] # the name of the song Singer_name = music ['singer'] [0] [' name'] # singer's name songmid = music ['songmid'] media_mid = music [' media_mid'] music_info_list.append ((music_name) Singer_name, songmid, media_mid) # get vkey music_data = [] for music in music_info_list: music_name = music [0] singer_name = music [1] songmid = music [2] url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch", "param": {"guid": "8846039534", "calltype": 0, "userip": ""}}, "req_0": {"module": "vkey.GetVkeyServer", "method": "CgiGetVkey", "param": {"guid": "8846039534", "songmid": [% s "]," songtype ": [0]," uin ":" 115292150478213523 "," loginflag ": 1," platform ":" 20 "}}," comm ": {" uin ":" 1152921504784213523 "," format ":" json " "ct": 24 "cv": 0}}'% songmid response = requests.get (url). Json () # if the data you get is {} .json (), he will directly convert it into the dictionary purl = response ['req_0'] [' data'] ['midurlinfo'] [0] [' purl'] full_media_url = 'http://dl.stream.qqmusic.qq.com/' + purl Music_data.append ({'music_name': music_name 'singer_name': singer_name 'full_media_url': full_media_url}) if not os.path.exists (' song download'): os.mkdir ('song download') for music in music_data: music_name = music ['music_name'] singer_name = music [' singer_name'] full_url = music ['full _ media_url'] music_response = requests.get (full_url Headers=headers) .content # gets the binary data with open ('song download /% smurt% s.mp3'% (music_name, singer_name), 'wb') as fp: fp.write (music_response) # add data to the last text.insert (END) of the list box 'downloading:% s'% music_name) # text box scrolling down text.see (END) # update (if not updated or stuck there all the time) Show the same content) text.update () # 1. Create a window root = Tk () # 2. Window title root.title ('QQ Music') # 3. Window size and display position, with lowercase xroot.geometry ('550x400' 550 '230') in the middle # window display position # 4. Label control lable = Label (root, text=', please enter the singer or song you need to download:', font= ('Microsoft Yahi', 10)) lable.grid (row=0, column=0) # 5. Input control entry = Entry (root, font= ('Microsoft Yahei', 25)) entry.grid (row=0, column=1) # 6. List box control text = Listbox (root, font= ('Microsoft Yahei', 16), width=45, height=10) # # the number of columns across months of the columnspan component text.grid (row=1, columnspan=2) # 7. Button controls button = Button (root, text=' start downloading', width=10, font= ('Microsoft Yahue', 10), command=downlaod_song) button.grid (row=2, column=0, sticky=W) button1 = Button (root, text=' exit', width=10, font= ('Microsoft Yahue', 10), command=root.quit) button1.grid (row=2, column=1, sticky=E) # message loop, display window root.mainloop () 4) effect display
Take Li XX as an example to download the song:
Download whatever you want.
After reading the above, do you have any further understanding of how Python implements a free music downloader? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.