Is it too much trouble downloading songs? Create your own music Downloader

Preface We have made a music player that belongs to us. How can we not have a music downloader Have you ever downloaded ...

Preface

We have made a music player that belongs to us. How can we not have a music downloader

Have you ever downloaded MP3 files of songs from your computer and put them into the memory card of your mobile phone before? With the development of the times, now the major music software has become a player. Downloading music is for a fee. Now we can use python to crawl music and build our own music downloader from scratch.

Knowledge points:
1.python basic knowledge
2.requests Library
3. Urlib Library
4.BeautifulSoup

Environmental Science:

windows + pycharm + python3

Suitable for students with zero Foundation

1. Import tool

import os from urllib.request import urlretrieve from tkinter import * import requests from selenium import webdriver

2. Interface

# Create interface root = Tk() # Title root.title('Netease cloud music Downloader') # Set window size root.geometry('560x450') # Label control label = Label(root,text='Please enter a song name:',font=('Chinese Xingkai',20)) # Label positioning label.grid() # Input box entry = Entry(root,font=('Official script',20)) entry.grid(row=0,column=1) # list box text = Listbox(root,font=('Regular script',16),width=50,heigh=15) text.grid(row=1,columnspan=2) # Go across # Start button button = Button(root,text='Start downloading',font=('Official script',15),command=get_music_name) #command button.grid(row=2,column=0,sticky=W) #sticky Alignment W E N S # Exit button button1 = Button(root,text='Exit program',font=('Official script',15),command=root.quit) #command button1.grid(row=2,column=1,sticky=E) # Display interface root.mainloop()

Run the code and get only one interface

3. Function

Access to Netease cloud music

# https://music.163.com/#/search/m/?s=%E7%9B%97%E5%B0%86%E8%A1%8C&type=1 # http://music.163.com/song/media/outer/url?id=574566207.mp3 headers = { 'Referer': 'https://music.163.com/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36' }

Download songs

def song_load(item): song_id = item['song_id'] song_name = item['song_name'] song_url = 'http://music.163.com/song/media/outer/url?id={}.mp3'.format(song_id) # create folder os.makedirs('music',exist_ok=True) path = 'music\{}.mp3'.format(song_name) # Text box text.insert(END,'song:{},Downloading...'.format(song_name)) # Text box scrolling text.see(END) # To update text.update() # download urlretrieve(song_url,path) # Text box text.insert(END, 'Download completed:{},Please listen...'.format(song_name)) # Text box scrolling text.see(END) # To update text.update()

Search song name

def get_music_name(): name = entry.get() url = 'https://music.163.com/#/search/m/?s={}&type=1'.format(name) # Hide browser option = webdriver.ChromeOptions() option.add_argument('--headless') driver = webdriver.Chrome(chrome_options=option) driver.get(url=url) driver.switch_to.frame('g_iframe') # Get songs id req = driver.find_element_by_id('m-search') a_id = req.find_element_by_xpath('.//div[@class="item f-cb h-flag "]/div[2]//a').get_attribute("href") print(a_id) song_id = a_id.split('=')[-1] print(song_id) # Get song name song_name = req.find_element_by_xpath('.//div[@class="item f-cb h-flag "]/div[2]//b').get_attribute("title") print(song_name) item = {} item['song_id'] = song_id item['song_name'] = song_name driver.quit() # Exit browser song_load(item)

Finally, run the code, the effect is as follows

If you want to learn Python or are learning python, there are many Python tutorials, but are they up to date? Maybe you have learned something that someone else probably learned two years ago. Share a wave of the latest Python tutorials in 2020 in this editor. Access to the way, private letter small "information", you can get free Oh!

7 May 2020, 11:23 | Views: 1371

Add new comment

For adding a comment, please log in
or create account

0 comments