import requests
import json
# 爬蟲原理 模擬瀏覽器 獲取請求數據
#點擊播放連接
#url = "https://www.ximalaya.com/revision/play/album?albumId=297790&pageNum=1&sort=1&pageSize=30"
url = "https://www.ximalaya.com/revision/play/album?albumId=333001&pageNum=1&sort=1&pageSize=30"
#保存的身份信息
header = {
???? "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36"
}
#獲取url的內容放在response 獲得json格式的字符串.
response = requests.get(url,headers=header).text
#print(response)
#將json格式轉化為字典數據
audio_data = json.loads(response)["data"]["tracksAudioPlay"]
print(audio_data)
#獲取每一個元素獲取src連接
for audio_info in audio_data:
???? music_url = audio_info["src"]
???? music_name1= audio_info["trackName"]
? # print(audio_info)
?? #切片分割字典拿出名字
???? music_name = music_url.split("/")[-1]
???? print(music_name1) #音頻文件的名稱
???? print(music_name)? #url中m4a音頻文件的名稱
???? print(music_url) #音頻文件的鏈接
??? #保存在music文件下的music_name 格式化輸出 參數: 路徑占位符 ,format名字 ,文件格式? 去別名f
???? with open("music1/"+ music_name1 + ".mp3","wb")as f:? #這里創建文件名稱 : 兄弟? 朋友 愛情 掙錢
???? #???? #音頻內容從哪里獲得
???????? f.write(requests.get(music_url).content)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
