You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
8.4 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"开始下载第1首音乐勇气.mp3\n",
"\n",
"开始下载第2首音乐可惜不是你.mp3\n",
"\n",
"开始下载第3首音乐宁夏.mp3\n",
"\n",
"开始下载第4首音乐接受.mp3\n",
"\n",
"开始下载第5首音乐分手快乐.mp3\n",
"\n",
"开始下载第6首音乐明明很爱你.mp3\n",
"\n",
"开始下载第7首音乐爱久见人心.mp3\n",
"\n",
"开始下载第8首音乐夜夜夜夜.mp3\n",
"\n",
"开始下载第9首音乐可以的话.mp3\n",
"\n",
"开始下载第10首音乐丝路.mp3\n",
"\n",
"开始下载第11首音乐会过去的.mp3\n",
"\n",
"开始下载第12首音乐爱你不是两三天.mp3\n",
"\n",
"开始下载第13首音乐如果有一天.mp3\n",
"\n",
"开始下载第14首音乐偶阵雨.mp3\n",
"\n",
"开始下载第15首音乐无条件为你.mp3\n",
"\n",
"开始下载第16首音乐慢慢来比较快.mp3\n",
"\n",
"开始下载第17首音乐听不到.mp3\n",
"\n",
"开始下载第18首音乐燕尾蝶.mp3\n",
"\n",
"开始下载第19首音乐昨天.mp3\n",
"\n",
"开始下载第20首音乐Fly Away.mp3\n",
"\n",
"开始下载第21首音乐孤单北半球 (live).mp3\n",
"\n",
"开始下载第22首音乐小手拉大手(Live).mp3\n",
"\n",
"开始下载第23首音乐我喜欢.mp3\n",
"\n",
"开始下载第24首音乐飘洋过海来看你.mp3\n",
"\n",
"开始下载第25首音乐没有人像你.mp3\n",
"\n",
"开始下载第26首音乐不想睡.mp3\n",
"\n",
"开始下载第27首音乐情歌没有告诉你.mp3\n",
"\n",
"开始下载第28首音乐大地之歌.mp3\n",
"\n",
"开始下载第29首音乐瘦瘦的.mp3\n",
"\n",
"开始下载第30首音乐还是好朋友.mp3\n",
"\n",
"开始下载第31首音乐你会不会.mp3\n",
"\n",
"开始下载第32首音乐一路两个人.mp3\n",
"\n",
"开始下载第33首音乐一夜长大.mp3\n",
"\n",
"开始下载第34首音乐她.mp3\n",
"\n",
"开始下载第35首音乐第三者.mp3\n",
"\n",
"开始下载第36首音乐最想环游的世界.mp3\n",
"\n",
"开始下载第37首音乐美丽人生.mp3\n",
"\n",
"开始下载第38首音乐纯真.mp3\n",
"\n",
"开始下载第39首音乐我不害怕.mp3\n",
"\n",
"开始下载第40首音乐彩虹.mp3\n",
"\n",
"开始下载第41首音乐想都没想过.mp3\n",
"\n",
"开始下载第42首音乐最快乐那一年.mp3\n",
"\n",
"开始下载第43首音乐我就知道那是爱.mp3\n",
"\n",
"开始下载第44首音乐漂洋过海来看你(Live).mp3\n",
"\n",
"开始下载第45首音乐让爱转动整个宇宙.mp3\n",
"\n",
"开始下载第46首音乐我还记得.mp3\n",
"\n",
"开始下载第47首音乐有你在.mp3\n",
"\n",
"开始下载第48首音乐向左转向右转.mp3\n",
"\n",
"开始下载第49首音乐明天我要嫁给你了(Live).mp3\n",
"\n",
"开始下载第50首音乐恋着多喜欢.mp3\n",
"\n",
"50首全部歌曲已经下载完毕\n"
]
}
],
"source": [
"import os\n",
"import re\n",
"import json\n",
"import requests\n",
"from lxml import etree\n",
"\n",
"\n",
"def download_songs(url=None):\n",
" if url is None:\n",
" url = 'https://music.163.com/#/playlist?id=2384642500'\n",
"\n",
" url = url.replace('/#', '').replace('https', 'http') # 对字符串进行去空格和转协议处理\n",
" # 网易云音乐外链url接口http://music.163.com/song/media/outer/url?id=xxxx\n",
" out_link = 'http://music.163.com/song/media/outer/url?id='\n",
" # 请求头\n",
" headers = {\n",
" 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',\n",
" 'Referer': 'https://music.163.com/',\n",
" 'Host': 'music.163.com'\n",
" }\n",
" # 请求页面的源码\n",
" res = requests.get(url=url, headers=headers).text\n",
"\n",
" tree = etree.HTML(res)\n",
" # 音乐列表\n",
" song_list = tree.xpath('//ul[@class=\"f-hide\"]/li/a')\n",
" # 如果是歌手页面\n",
" artist_name_tree = tree.xpath('//h2[@id=\"artist-name\"]/text()')\n",
" artist_name = str(artist_name_tree[0]) if artist_name_tree else None\n",
"\n",
" # 如果是歌单页面:\n",
" #song_list_tree = tree.xpath('//*[@id=\"m-playlist\"]/div[1]/div/div/div[2]/div[2]/div/div[1]/table/tbody')\n",
" song_list_name_tree = tree.xpath('//h2[contains(@class,\"f-ff2\")]/text()')\n",
" song_list_name = str(song_list_name_tree[0]) if song_list_name_tree else None\n",
"\n",
" # 设置音乐下载的文件夹为歌手名字或歌单名\n",
" folder = './' + artist_name if artist_name else './' + song_list_name\n",
"\n",
" if not os.path.exists(folder):\n",
" os.mkdir(folder)\n",
"\n",
" for i, s in enumerate(song_list):\n",
" href = str(s.xpath('./@href')[0])\n",
" song_id = href.split('=')[-1]\n",
" src = out_link + song_id # 拼接获取音乐真实的src资源值\n",
" title = str(s.xpath('./text()')[0]) # 音乐的名字\n",
" filename = title + '.mp3'\n",
" filepath = folder + '/' + filename\n",
" print('开始下载第{}首音乐:{}\\n'.format(i + 1, filename))\n",
"\n",
" try: # 下载音乐\n",
" #下载歌词\n",
" #download_lyric(title, song_id)\n",
"\n",
" data = requests.get(src).content # 音乐的二进制数据\n",
"\n",
" with open(filepath, 'wb') as f:\n",
" f.write(data)\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" print('{}首全部歌曲已经下载完毕!'.format(len(song_list)))\n",
"\n",
"\n",
"def download_lyric(song_name, song_id):\n",
" url = 'http://music.163.com/api/song/lyric?id={}&lv=-1&kv=-1&tv=-1'.format(song_id)\n",
" # 请求头\n",
" headers = {\n",
" 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',\n",
" 'Referer': 'https://music.163.com/',\n",
" 'Host': 'music.163.com'\n",
" # 'Origin': 'https://music.163.com'\n",
" }\n",
" # 请求页面的源码\n",
" res = requests.get(url=url, headers=headers).text\n",
" json_obj = json.loads(res)\n",
" lyric = json_obj['lrc']['lyric']\n",
" reg = re.compile(r'\\[.*\\]')\n",
" lrc_text = re.sub(reg, '', lyric).strip()\n",
"\n",
" print(song_name, lrc_text)\n",
"\n",
"\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" #music_list = 'https://music.163.com/#/playlist?id=2384642500' #歌曲清单\n",
" music_list = 'https://music.163.com/#/artist?id=8325' #歌手排行榜\n",
" # music_list = 'https://music.163.com/#/search/m/?order=hot&cat=全部&limit=435&offset=435&s=梁静茹' #搜索列表\n",
" download_songs(music_list)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}