python爬蟲實例--網易雲音樂排行榜爬蟲

網易雲音樂,之前是有個api 連接的json下載的,如今沒了, 只有音樂id,title , 只能看播放請求了, 可是播放請求都是加密的值,好坑...python

進過各類努力, 終於找到了個大神寫的博客,3.6版本的pythonc++

   python 完美破解網易雲音樂: https://segmentfault.com/a/1190000012818254 git

      python 代碼下載地址:  https://github.com/imyxuan/Neteasegithub

在運行大神的代碼時遇到了各類錯誤:json

   from Crypto.Cipher import AESsegmentfault

  須要安裝: pip install pycryptoapi

    第一次錯誤: 提示沒有安裝Visual Studio 2015 Microsoft Build Tools 14.0     app

      下載地址:   https://www.microsoft.com/en-us/download/details.aspx?id=48159工具

     第二次錯誤: 安裝vs 又提示.NET Framework 版本低,因而又安裝4.6版本          post

      下載地址:   https://www.microsoft.com/en-us/download/details.aspx?id=48130

    第三次錯誤: 使用cmd 安裝pip install pycrypto  提示cl.exe 運行沒法正常運行,繼續各類百度, 下載個c++的修復軟件Directx: 

      下載地址:   http://soft.duote.org/directx_3.7.zip

    運行修復工具修復完了後提示c++有異常 ,須要擴展修復.....而後工具-->選項-->擴展-->開始擴展.而後再修復一遍,提示c++修復成功!!

    第四次錯誤: 本覺得這樣就行了,mmp , cmd 運行又出錯了 

        intmax_t C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.

      只能百度了:https://blog.csdn.net/u010377372/article/details/78470824  ,安裝這個弄的:

        解決方案:

        1.設置Microsoft Visual Studio 14.0的環境變量 VCINSTALLDIR 變量值 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC(默認安裝位置,請根據本身的安裝位置肯定)如圖:

 

      2.在上面安裝路徑中執行 vcvarsall.bat 腳本

      3.在命令行(cmd)中執行 set CL=-FI"%VCINSTALLDIR%\INCLUDE\stdint.h"

      4.從新安裝 pip install pycrypto

     終於安裝成功了, 而後信心滿滿的重啓了個人PyCharm, 結果運行的時候仍是找不到那個Crypto模塊,這是已通過去好幾個小時了沒辦法, 只能百度了..

    檢查了python的模塊安裝目錄, 發現已經存在了Crypto, 可是項目裏面加載不了, 應該是項目裏面的引用不對, 果真, 項目裏面沒有那個模塊, 因而, 把安裝的Crypto複製一份到了項目的目錄下, 此時完美解決了  

    python 的Crypto 安裝目錄: C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Lib\site-packages

    項目的python 模塊目錄: 

      

 

                將安裝目錄下的site-packages全部文件複製到項目的site-packages去, 就完美解決了;歡迎加入QQ: 1095737364 QQ羣:123300273

 

下面是代碼: 

 

# coding: utf-8
#https://segmentfault.com/a/1190000012818254  聲明: 是這個大神寫的, 和沒啥關係,我只是改了改
from Crypto.Cipher import AES
import base64
import requests
import sys

headers = {
    'Cookie': 'appver=1.5.0.75771;',
    'Referer': 'http://music.163.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}

first_param = "{\"ids\":\"[%d]\",\"br\":128000,\"csrf_token\":\"\"}"
second_param = "010001"
third_param = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"
forth_param = "0CoJUm6Qyw8W8jud"


def get_params():
    iv = "0102030405060708"
    first_key = forth_param
    second_key = 16 * 'F'
    h_encText = AES_encrypt(first_param, first_key, iv)
    h_encText = AES_encrypt(h_encText, second_key, iv)
    return h_encText


def get_encSecKey():
    encSecKey = "257348aecb5e556c066de214e531faadd1c55d814f9be95fd06d6bff9f4c7a41f831f6394d5a3fd2e3881736d94a02ca919d952872e7d0a50ebfa1769a7a62d512f5f1ca21aec60bc3819a9c3ffca5eca9a0dba6d6f7249b06f5965ecfff3695b54e1c28f3f624750ed39e7de08fc8493242e26dbc4484a01c76f739e135637c"
    return encSecKey


def AES_encrypt(text, key, iv):
    pad = 16 - len(text) % 16
    if isinstance(text, str):
        text = text + pad * chr(pad)
    else:
        text = text.decode('utf-8') + pad * chr(pad)
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    encrypt_text = encryptor.encrypt(text)
    encrypt_text = base64.b64encode(encrypt_text)
    return encrypt_text


def get_json(url, params, encSecKey):
    data = {
        "params": params,
        "encSecKey": encSecKey
    }
    response = requests.post(url, headers=headers, data=data).json()
    return response['data']

# 榜單歌曲批量下載
# r = requests.get('http://music.163.com/api/playlist/detail?id=2884035')  # 網易原創歌曲榜
# r = requests.get('http://music.163.com/api/playlist/detail?id=19723756') # 雲音樂飆升榜
# r = requests.get('http://music.163.com/api/playlist/detail?id=3778678')  # 雲音樂熱歌榜
#r = requests.get('http://music.163.com/api/playlist/detail?id=3779629')    # 雲音樂新歌榜

# 歌單歌曲批量下載
# r = requests.get('http://music.163.com/api/playlist/detail?id=123415635')    # 雲音樂歌單——【華語】中國風的韻律,中國人的印記
# r = requests.get('http://music.163.com/api/playlist/detail?id=122732380')    # 雲音樂歌單——那不是愛,只是寂寞說的謊

r=requests.get("http://music.163.com/api/playlist/detail?id=2884035",headers=headers)




arr=r.json()['result']['tracks']
for i in range(100):
    toplistMP3ID =str(arr[i]['id'])
    toplistMP3Title = str(arr[i]['name'])

    music_id = toplistMP3ID
    first_param = "{\"ids\":\"[%d]\",\"br\":128000,\"csrf_token\":\"\"}" % int(music_id)
    url = 'https://music.163.com/weapi/song/enhance/player/url?csrf_token='
    params = get_params()
    encSecKey = get_encSecKey()
    """
    rsp:{
        'data': [{
            'gain': 2.3073,
            'type': 'mp3',
            'url': 'http://m10.music.126.net/20180111133509/24c79548414f7aa7407985818cb16a39/ymusic/333c/66b1/e5ec/
                72aeb13aca24c989295e58e8384e3f97.mp3',
            'md5': '72aeb13aca24c989295e58e8384e3f97',
            'flag': 0, 'code': 200, 'payed': 0, 'id': 151619, 'expi': 1200, 'size': 3868307,
            'uf': None, 'br': 128000, 'fee': 0, 'canExtend': False}], 'code': 200}
    """
    rsp = get_json(url, params, encSecKey)
    music_url = rsp[0].get('url')
    if music_url:
        music = requests.get(music_url)
        name = sys.path[0] + "/mp3/%s.mp3" % toplistMP3Title
        print(name)
        with open(name, "wb") as code:
            code.write(music.content)


# music_id = input('請輸入歌曲ID:')
相關文章
相關標籤/搜索