閑得無聊,到處找推薦歌曲都沒有滿意的,想了想不是有爬蟲嗎,反手就把酷狗排行榜的歌都給它爬下來了,不說了,我聽歌去了~

文末的話,我也放了相關視頻教程,比文章詳細多了

開始今天的正文吧
這是今天的知識點
1、動態數據抓包演示 2、json數據解析 3、requests模塊的使用 4、正則表達式的簡單使用 5、css選擇器數據解析方法
用到的環境和模塊
python 3.6 pycharm (安裝包/安裝教程/激活碼/使用教程/翻譯插件) requests >>> pip install requests parsel >>> pip install parsel re 正則表達式
爬蟲可見即可爬,你要想爬所有的也可以。
思路流程:
一. 數據來源分析
1. 確定需求 爬取音樂數據 音頻 2. 通過開發者工具(瀏覽器都自帶 F12 或者 鼠標右鍵點擊檢查)進行抓包分析 I. 找我們音樂播放地址 play_url II. 找播放地址來源 對比參數變化 [hash ID]1629808844252 時間戳(表示你請求的時間點) time 時間模塊 可以獲取當前時間戳 III. 找 hash ID 參數的來源 IV. 獲取所有榜單的url地址
二. 代碼實現過程
1. 發送請求 對於榜單url地址發送請求 2. 獲取數據 獲取網頁源代碼數據 3. 解析數據 提取所有榜單相對應的url地址 4. 發送請求 對於 榜單的url地址發送請求 5. 獲取數據 獲取網頁源代碼數據 6. 解析數據 提取音樂 hash 和 id 值 7. 發送請求 把 hash 和 id 值 參數相對url里面 發送請求 8. 獲取數據 獲取json字典數據 9. 解析數據 提取 歌名 音樂播放地址 10. 保存數據
數據來源分析 >>> 發送請求 >>> 獲取數據 >>> 解析數據 >>> 保存數據
首先把要用的模塊都安排上
import requests # 數據請求模塊 pip install requests import parsel # 數據解析模塊 pip install parsel import re # 正則表達式 內置模塊 不需要安裝 import pprint # 格式化輸出 import os
設置一手文件夾,我們爬下來的音樂就放到這里
filename = 'music\\' if not os.path.exists(filename): os.mkdir(filename)
然后有些歌曲下載下來可能有特殊字符,我們得把它替換掉
def change_title(title): pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]") # '/ \ : * ? " < > |' new_title = re.sub(pattern, "_", title) # 替換為下划線 return new_title
1、發送請求 對於榜單url地址發送請求
url = 'https://www.kugou.com/yy/html/rank.html'
請求頭 作用: 偽裝 把python代碼偽裝成瀏覽器發送請求
任意一個數據包的 user-agent 都是一樣的
user-agent 表示的就是瀏覽器的基本信息
headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36' } response = requests.get(url=url, headers=headers)
2、獲取數據 獲取網頁源代碼數據
print(response.text)
html 字符串數據(想直接解析字符串數據 只能用re) 轉成 selector 對象
3、解析數據 提取所有榜單相對應的url地址
selector = parsel.Selector(response.text) print(selector)
css選擇器 根據標簽的內容 提取想要的數據
第一次提取 獲取li 標簽
lis = selector.css('.pc_rank_sidebar li') lis = lis[13:]
返回的是列表, 所以可以遍歷 把里面每一個元素提取出來 Selector 對象
for li in lis: title = li.css('a::attr(title)').get() link_url = li.css('a::attr(href)').get() print(f'=====================正在爬取{title}=====================') print(title, link_url)
4、發送請求 對於 榜單的url地址發送請求
response_1 = requests.get(url=link_url, headers=headers) re.findall('')
5、獲取數據 獲取網頁源代碼數據 response_1.text
print(response_1.text)
6、解析數據 提取音樂 hash 和 id 值
hash_list = re.findall('"Hash":"(.*?)"', response_1.text)
\d 匹配一個數字 \d+ 匹配多個數字 .*? 可以匹配任意字符 (除了\n)
正則表達式匹配的數據 返回的是列表
album_id = re.findall('"album_id":(\d+),', response_1.text) for index in zip(hash_list, album_id): hash = index[0] music_id = index[1]
7、 發送請求 把 hash 和 id 值 參數相對url里面 發送請求
index_url = 'https://wwwapi.kugou.com/yy/index.php' params = { 'r': 'play/getdata', # 'callback': 'jQuery19106964302346548317_1629810585326', 'hash': hash, 'dfid': '1JdWoI2IQjNS2aq9KB1Ylhf3', 'mid': 'fe0e97001229790f9065ef29dec3bdcd', 'platid': '4', 'album_id': music_id, '_': '1629810585327', } response_2 = requests.get(url=index_url, params=params, headers=headers) # json_data = response_2.json()['data'] music_name = response_2.json()['data']['audio_name'] new_name = change_title(music_name) music_url = response_2.json()['data']['play_url'] if music_url: music_content = requests.get(url=music_url, headers=headers).content with open(filename + new_name + '.mp3', mode='wb') as f: f.write(music_content) print(music_name)

來看看效果


這下子我又能消停幾天了
視頻教程我也放在這給大家了,視頻會講的詳細一些 點我看視頻 密碼:qwer
