Python破解网易云音乐下载版权限制

在播放某个音乐时,网易云音乐会通过使用其api来解析这首音乐的id。
用Eminem的Rap God来举例。

Rap God by Eminem

https://music.163.com/#/song?id=27853227
#注意其中显示此音乐的id为27853227

网易云用于解析id的api

https://music.163.com/song/media/outer/url?id=[id号]
Rap God: https://music.163.com/song/media/outer/url?id=27853227

访问这个链接,会直接跳转到这首歌的下载地址

https://m10.music.126.net/20181222125620/84340481235d7563b80a0536ecb6adb6/ymusic/2057/c470/618a/511a158c95fa942e8eeb0c6171684652.mp3
#注:由于这个站点会检查Referer,直接访问这个链接是没有用的,要通过上面的api来访问

根据我的多次实践,绝大多数的浏览器并不会直接下载这个音乐文件,而是直接在浏览器中播放,并且是没有下载选项的。
但是由于暴露出了文件位置,我们可以使用爬虫来下载此音乐。大致思路是先访问api,从返回的头信息中获取要跳转的Location,然后利用urllib.urlretrieve()来下载音乐文件。


定义headers

headers = { 'User-agent':
            'Mozilla/5.0 (X11; Linux x86_64; rv:57.0)Gecko/20100101 Firefox/57.0',
            'Host':'music.163.com',
            'Referer':'https://music.163.com'}

访问api

import requests
ID = '27853227'
url = 'https://music.163.com/song/media/outer/url?id='
req = requests.get(url+ID, headers=headers, allow_redirects=False)

注意,如果不加headers,会返回状态码200并伴随警告信息

>>> req = requests.get('https://music.163.com/song/media/outer/url?id=27853227')
>>> req
<Response [200]>
>>> req.text
u'{"code":-460,"msg":"Cheating"}'

还有,如果不加allow_redirects=False,会直接报错。这是由于requests库是默认禁止302跳转的

>>> req = requests.get('https://music.163.com/song/media/outer/url?id=27853227',headers=headers)
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 67, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 53, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 597, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 195, in resolve_redirects
    **adapter_kwargs
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 426, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

获取链接并使用urllib下载

import urllib
musicLink = req.headers['Location']
urllib.urlretrieve(musicLink, 'music/rapgod.mp3') 

完整代码

import urllib, requests
headers = { 'User-agent':
            'Mozilla/5.0 (X11; Linux x86_64; rv:57.0)Gecko/20100101 Firefox/57.0',
            'Host':'music.163.com',
            'Referer':'https://music.163.com'}

ID = '27853227'
url = 'https://music.163.com/song/media/outer/url?id='
req = requests.get(url+ID, headers=headers, allow_redirects=False)
musicLink = req.headers['Location']

urllib.urlretrieve(musicLink,"music/rapgod.mp3")

新手小白第一次发文,非喜误喷

本文用于进行技术交流,严禁任何个人或组织将其用于违法用途

你可能感兴趣的:(python)