{ "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 }