前言
最近很多网易的音乐都听不到了。 刚刚看了很多教程。 我跟进研究了一下。 方式!
Python+爬虫
首先,我们来谈谈我们打算做什么:
http://music.163.com/song/media/outer/url?id='
id是歌曲的id!
所以,我们爬虫现在主要的工作就是找到这个ID。 当然,为了更好的保存,我们还必须找到歌名!
现在我们需要找到我们需要抓取的网站链接! 我分析了一下安卓源码下载音乐网站,发现大概有以下三种:
#歌曲清单
music_list = 'https://music.163.com/#/playlist?id=2412826586'
#歌手排行榜
artist_list = 'https://music.163.com/#/artist?id=8325'
#搜索列表
search_list = 'https://music.163.com/#/search/m/?order=hot&cat=全部&limit=435&offset=435&s=梁静茹'
如果你只是想下载一首歌曲,比如静茹-勇气:#/song?id=254485,那么用浏览器打开就可以了,不需要爬虫!
下载歌词
如果您还想下载歌词,也很简单。 只需使用插孔并获得歌曲 ID:
url = 'http://music.163.com/api/song/lyric?id={}&lv=-1&kv=-1&tv=-1'.format(song_id)
返回的json数据是这样的
{
sgc: true,
sfy: false,
qfy: false,
lrc:
{
version: 7,
lyric: "[00:39.070]开了窗 等待天亮n[00:46.160]看这城市 悄悄的 熄了光n[00:51.850]听风的方向n[00:55.090]这一刻 是否和我一样n[00:58.730]孤单的飞翔n[01:02.300]模糊了眼眶n[01:07.760]广播里 那首歌曲n[01:14.830]重复当时 那条街那个你n[01:20.410]相同的桌椅n[01:23.740]不用言语 就会有默契n[01:27.470]这份亲密n[01:30.560]那么熟悉n[01:33.850]在爱里 等着你n[01:37.480]被你疼惜 有种暖意n[01:41.090]在梦里 全是你n[01:43.920]不要再迟疑 把我抱紧"
},
klyric:
{
version: 0,
lyric: null
},
tlyric:
{
version: 0,
lyric: null
},
code: 200
}
坑与进步
表面上很简单,但是需要注意的是,网易返回的链接数据是通过js动态加载的,即爬虫获取到的网页数据与浏览器获取到的DOM内容和结构是不同的!
坑
其中,搜索列表爬虫返回的内容根本无法获取歌曲ID! ! !
解决
也有解决办法!
Python模拟浏览器
使用selenium+phantomjs无界面浏览器安卓源码下载音乐网站,两者结合虽然是直接操作浏览器,但是可以获得JavaScript渲染的页面数据。
缺点
由于是无界面浏览器,这种方案效率极低,如果要大量爬取的话不推荐。
对于异步请求且源代码中不存在数据的情况,捕获数据也比较困难。
在播放列表中搜索歌曲
例如,如果您想下载某个歌手的所有音乐,使用手机云音乐搜索,然后将它们全部保存到新的播放列表中,就可以了!
总结
使用python的时候一定要简单。 我认为复杂的事情尽量少做,尽可能使用技巧,所以本文不使用selenium+phantomjs进行练习。
注:本文仅供技术交流,请勿用于商业用途~如有违规,本人概不负责。
所有代码
又一个非常简单的100行代码就完成了! ! !
import os
import re
import json
import requests
from lxml import etree
def download_songs(url=None):
if url is None:
url = 'https://music.163.com/#/playlist?id=2384642500'
url = url.replace('/#', '').replace('https', 'http') # 对字符串进行去空格和转协议处理
# 网易云音乐外链url接口:http://music.163.com/song/media/outer/url?id=xxxx
out_link = 'http://music.163.com/song/media/outer/url?id='
# 请求头
headers = {
'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',
'Referer': 'https://music.163.com/',
'Host': 'music.163.com'
}
# 请求页面的源码
res = requests.get(url=url, headers=headers).text
tree = etree.HTML(res)
# 音乐列表
song_list = tree.xpath('//ul[@class="f-hide"]/li/a')
# 如果是歌手页面
artist_name_tree = tree.xpath('//h2[@id="artist-name"]/text()')
artist_name = str(artist_name_tree[0]) if artist_name_tree else None
# 如果是歌单页面:
#song_list_tree = tree.xpath('//*[@id="m-playlist"]/div[1]/div/div/div[2]/div[2]/div/div[1]/table/tbody')
song_list_name_tree = tree.xpath('//h2[contains(@class,"f-ff2")]/text()')
song_list_name = str(song_list_name_tree[0]) if song_list_name_tree else None
# 设置音乐下载的文件夹为歌手名字或歌单名
folder = './' + artist_name if artist_name else './' + song_list_name
if not os.path.exists(folder):
os.mkdir(folder)
for i, s in enumerate(song_list):
href = str(s.xpath('./@href')[0])
song_id = href.split('=')[-1]
src = out_link + song_id # 拼接获取音乐真实的src资源值
title = str(s.xpath('./text()')[0]) # 音乐的名字
filename = title + '.mp3'
filepath = folder + '/' + filename
print('开始下载第{}首音乐:{}n'.format(i + 1, filename))
try: # 下载音乐
#下载歌词
#download_lyric(title, song_id)
data = requests.get(src).content # 音乐的二进制数据
with open(filepath, 'wb') as f:
f.write(data)
except Exception as e:
print(e)
print('{}首全部歌曲已经下载完毕!'.format(len(song_list)))
def download_lyric(song_name, song_id):
url = 'http://music.163.com/api/song/lyric?id={}&lv=-1&kv=-1&tv=-1'.format(song_id)
# 请求头
headers = {
'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',
'Referer': 'https://music.163.com/',
'Host': 'music.163.com'
# 'Origin': 'https://music.163.com'
}
# 请求页面的源码
res = requests.get(url=url, headers=headers).text
json_obj = json.loads(res)
lyric = json_obj['lrc']['lyric']
reg = re.compile(r'[.*]')
lrc_text = re.sub(reg, '', lyric).strip()
print(song_name, lrc_text)
if __name__ == '__main__':
#music_list = 'https://music.163.com/#/playlist?id=2384642500' #歌曲清单
music_list = 'https://music.163.com/#/artist?id=8325' #歌手排行榜
# music_list = 'https://music.163.com/#/search/m/?order=hot&cat=全部&limit=435&offset=435&s=梁静茹' #搜索列表
download_songs(music_list)