Python爬取網易雲音樂歌單歌曲

窗邊冷月光發表於2018-04-17

注意點
網易雲音樂對請求引數做了加密

# 從網易雲音樂下載歌單歌曲
# 參考了這些網址
# https://blog.csdn.net/Ciiiiiing/article/details/62434438
# https://github.com/kunkun1230/Python-/tree/master/%E7%BD%91%E6%98%93%E4%BA%91%E9%9F%B3%E4%B9%90%20%E4%B8%8D%E5%86%8D%E7%8A%B9%E8%B1%AB%20%E8%AF%84%E8%AE%BA%E5%88%86%E6%9E%90
# https://www.zhihu.com/question/36081767

from Crypto.Cipher import AES
from bs4 import BeautifulSoup
import base64
import requests
import urllib.request
import os
import time

path = '../../files/網易雲音樂'
flag = os.path.exists(path)
if not flag:
    os.mkdir(path)

# 頭部資訊 #需根據自己瀏覽器的資訊進行替換
headers = {
        'Referer': 'http://music.163.com/',
        'Host': 'music.163.com',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    }

# 獲取引數
def get_params(text):
    first_key = "0CoJUm6Qyw8W8jud"
    second_key = "FFFFFFFFFFFFFFFF"
    h_encText = AES_encrypt(text, first_key)
    h_encText = AES_encrypt(h_encText, second_key)
    return h_encText

# 獲取 encSecKey
def get_encSecKey():
    encSecKey = "257348aecb5e556c066de214e531faadd1c55d814f9be95fd06d6bff9f4c7a41f831f6394d5a3fd2e3881736d94a02ca919d952872e7d0a50ebfa1769a7a62d512f5f1ca21aec60bc3819a9c3ffca5eca9a0dba6d6f7249b06f5965ecfff3695b54e1c28f3f624750ed39e7de08fc8493242e26dbc4484a01c76f739e135637c"
    return encSecKey


# 加密過程
def AES_encrypt(text, key):
    iv = "0102030405060708"
    pad = 16 - len(text) % 16
    text = text + pad * chr(pad)
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    encrypt_text = encryptor.encrypt(text)
    encrypt_text = base64.b64encode(encrypt_text)
    encrypt_text = str(encrypt_text, encoding="utf-8")  # 注意一定要加上這一句,沒有這一句則出現錯誤
    return encrypt_text

# 下載歌曲
def get_mp3(name, song_id):
    url = "http://music.163.com/weapi/song/enhance/player/url?csrf_token="  # 網易歌曲字首

    first_param = '{ids:"[%s]", br:"128000", csrf_token:""}' % song_id
    data = {
        "params": get_params(first_param).encode('utf-8'),
        "encSecKey": get_encSecKey()
    }

    try:
        response = requests.post(url, headers=headers, data=data)
        result = response.json()

        if result['code'] != 200:
            print('!!! 歌曲[%s]下載失敗...' % name)
            return

        mp3_url = result['data'][0]['url']
        urllib.request.urlretrieve(mp3_url, os.path.join('../../files/網易雲音樂', name + '.mp3'))
        print('歌曲[%s]下載完成...' % name)
    except:
        print('!!!歌曲[%s]下載出現異常...' % name)

if __name__ == "__main__":
    print('開始下載歌曲...\n================================================')
    start_time = time.time()  # 開始時間
    play_url = 'http://music.163.com/playlist?id=393565693'  # 歌單-民謠還在路上

    s = requests.session()
    s = BeautifulSoup(s.get(play_url, headers=headers).content, 'lxml')
    main = s.select('ul.f-hide li a')

    for music in main:
        song_id = music['href'][music['href'].find('id=') + len('id='):]
        # print('歌曲名稱:{},歌曲Id:{}'.format(music.text, song_id))
        get_mp3(music.text, song_id)

    end_time = time.time()  # 結束時間
    print("程式耗時%f秒." % (end_time - start_time))

相關文章