效果展示
爬取目標
網址:酷我音樂
工具使用
開發工具:pycharm
開發環境:python3.7, Windows10
使用工具包:requests,re
項目思路解析
找到需要解析的榜單數據
隨意點擊一個歌曲獲取到音樂的詳情數據 通過抓包的方式獲取到音樂播放數據
找到MP3的數據提交地址 mp3數據來自于這個url地址
提交數據的網址:
https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery19102816534571347611_1626783818555&hash=A38449E76C74D45825F565C1FDB825C0&dfid=3dKstH1sJdRa44o6Vj0ZIryF&mid=4458f6d567640b39de367a394d69879e&platid=4&album_id=40437970&_=1626783818556
將多個網址數據進行對比看看哪些參數是需要自行修改的
變化的url數據有3個
- hash
- album_id
- _
_ 可以明顯看出來是時間戳 需要獲取到對應的hash以及album_id的值 來到主頁找尋對應的歌曲id數據 發現數據來自網頁源代碼
歌曲的數據都是來自網頁源代碼
梳理整體思路:
- 從首頁源碼里提取出對應的hash、album_id值
- 組合成新的url地址
- 獲取到json數據總的歌曲播放地址
簡易源碼分享
本章內容只限學習,切勿用作其他用途!!!!!
import requests import re import time ?def Tools(url): headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.70' } response = requests.get(url, headers=headers) return response ?def Save(name, url): mp3 = Tools(url).content # 請求mp3地址鏈接 返回格式是16進制 f = open('./kugou/{}.mp3'.format(name), 'wb') # w 文件存在就寫入 不存在就會創建 b進制讀寫 f.write(mp3) f.close() print('{}下載完成....'.format(name)) ?url = 'https://www.kugou.com/yy/html/rank.html' response = Tools(url).text album_id = re.findall(r'"album_id":(\d*?),', response) # id Hash = re.findall(r'"Hash":"(.*?)",', response) # hash ?for a, h in zip(album_id, Hash): # 生成時間戳 time1 = int(time.time() * 1000) # 包含歌曲下載地址的url urls = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={}&dfid=0zlWqK0UWNFa0weUnX0hjlFa&mid=f79511e2e86914b99e351c42ba1f8bc7&platid=4&album_id={}&_={}'.format(h, a, time1) response1 = Tools(urls).json() audio_name = response1['data']['audio_name'].split('-')[1] play_url = response1['data']['play_url'] Save(audio_name, play_url) ?
以上就是Python實戰酷狗音樂反爬排行榜加密規則的詳細內容,更多關于Python反爬酷狗音樂排行榜加密規則的資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/AI19970205/article/details/118944328