使用python下载某易云歌曲
1、某易云的外链地址:
url = 'http://music.163.com/song/media/outer/url?id=' + 歌曲的id值 + '.mp3'
如:http://music.163.com/song/media/outer/url?id=1974443814.mp3
2、通过歌曲id下载:
import requests import os import bs4 from selenium import webdriver from time import sleep from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER' } # 创建保存音乐的文件夹 path = os.path.join('H:/网易云音乐下载/') if not os.path.exists(path): os.mkdir(path) # 输入音乐id id = input('请输入歌曲Id:') # 实现无可视化界面(固定写法) chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') # 初始化browser对象 service = Service(executable_path=r'E:\software\chromedriver_win32.exe') browser = webdriver.Chrome(service=service, options=chrome_options) # 获取音乐的id、名字,歌手名 def get_id_name_singer(url): browser.get(url=url) browser.switch_to.frame('g_iframe') sleep(3) page_text = browser.execute_script("return document.documentElement.outerHTML") soup = bs4.BeautifulSoup(page_text, 'html.parser') music_ids = soup.select("div[class='td w0'] a") # 音乐id music_id = music_ids[0].get("href") music_id = music_id.split('=')[-1] music_names = soup.select("div[class='td w0'] a b") # 音乐名字 music_name = music_names[0].get("title") music_singers = soup.select("div[class='td w1'] a") # 歌手名 music_singer = music_singers[0].string return music_id, music_name, music_singer # 下载音乐 def download_music(url, song_name, singer): response = requests.get(url=url, headers=headers) music_data = response.content music_path_name = '{}_{}.mp3'.format(song_name, singer) music_path = path + music_path_name with open(music_path, 'wb') as f: f.write(music_data) print(music_path_name, '下载成功') # 主函数 def main(): music_name = input('请输入歌曲名称:') music_singer = input('请输入歌手名称:') music_url = 'http://music.163.com/song/media/outer/url?id=' + id + '.mp3' download_music(music_url, music_name, music_singer) if __name__ == '__main__': main() browser.quit()
3、通过歌词名称下载:
歌曲搜索的页面地址:https://music.163.com/#/search/m/?id=1974443814&s=歌曲名称&type=1
具体代码实现:
import requests import os import bs4 from selenium import webdriver from time import sleep from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER' } # 创建保存音乐的文件夹 path = os.path.join('H:/网易云音乐下载/') if not os.path.exists(path): os.mkdir(path) # 输入音乐名 name = input('请输入歌名:')# 实现无可视化界面(固定写法) chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') # 初始化browser对象 service = Service(executable_path=r'E:\software\chromedriver_win32.exe') browser = webdriver.Chrome(service=service, options=chrome_options) # 获取音乐的id、名字,歌手名 def get_id_name_singer(url): browser.get(url=url) browser.switch_to.frame('g_iframe') sleep(3) page_text = browser.execute_script("return document.documentElement.outerHTML") soup = bs4.BeautifulSoup(page_text, 'html.parser') music_ids = soup.select("div[class='td w0'] a") # 音乐id music_id = music_ids[0].get("href") music_id = music_id.split('=')[-1] music_names = soup.select("div[class='td w0'] a b") # 音乐名字 music_name = music_names[0].get("title") music_singers = soup.select("div[class='td w1'] a") # 歌手名 music_singer = music_singers[0].string return music_id, music_name, music_singer # 下载音乐 def download_music(url, song_name, singer): response = requests.get(url=url, headers=headers) music_data = response.content music_path_name = '{}_{}.mp3'.format(song_name, singer) music_path = path + music_path_name with open(music_path, 'wb') as f: f.write(music_data) print(music_path_name, '下载成功') # 主函数 def main(): if name: url = 'https://music.163.com/#/search/m/?s=' + name + '&type=1' music_id, music_name, music_singer = get_id_name_singer(url) music_url = 'http://music.163.com/song/media/outer/url?id=' + music_id + '.mp3' download_music(music_url, music_name, music_singer)if __name__ == '__main__': main() browser.quit()
注:
chromedriver下载地址:http://chromedriver.storage.googleapis.com/index.html
希望大佬看到有不对的地方,提出博主予以改正!