生活随笔
收集整理的這篇文章主要介紹了
酷狗音乐的爬虫练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
酷狗音樂的爬蟲練習(只是作為新手練習,大神勿噴啊)
import requests
import json
import os
import time
import randommusic_name
= input('輸入要下載的歌曲名稱:')
url
= f'https://songsearch.kugou.com/song_search_v2?callback=jQuery1124042761514747027074_1580194546707&keyword={music_name}&page=1&pagesize=10&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1580194546709'
headers
= {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
response
= requests
.get
(url
,headers
=headers
)
html_data
= response
.text
html_info
= html_data
[html_data
.find
('(')+1:-2]
html_data_info
= json
.loads
(html_info
)
lists
= html_data_info
['data']['lists']
list_hash
= []
album_id_list
= []
list_id_info
= []
for list in lists
:SongName
= list['SongName'].replace
('<em>','').replace
('</em>','') singerName
= list['SingerName'].replace
('<em>','').replace
('</em>','')FileHash
= list['FileHash']AlbumID
= list['AlbumID']album_id_list
.append
(AlbumID
)list_hash
.append
(FileHash
) row
= {'歌手':singerName
,'歌名':SongName
}list_id_info
.append
(row
)print(list_hash
)
a
= 1
if len(list_id_info
) >= 1:for name
in list_id_info
:print(a
,name
)a
= a
+ 1all = input('是否需要下載全部歌曲:')if all == '是':for hash,albumid
in zip(list_hash
,album_id_list
):try:time
.sleep
(random
.randint
(2,5))movie_url
= f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={hash}&dfid=33ZWG24MjQ2x0kYBM53k7N6C&mid=2070c26ef7eeac6d59cb3372ce068810&platid=4&album_id={albumid}&_=1606976867986'print(movie_url
)response
= requests
.get
(url
=movie_url
, headers
=headers
).textresponse_json
= json
.loads
(response
) purl
= response_json
['data']['play_url']author_name
= response_json
['data']['author_name']song_name
= response_json
['data']['song_name']print(purl
)purl_data
= requests
.get
(url
=purl
, headers
=headers
).content
try:os
.mkdir
('酷狗音樂')except Exception
as e
:print(e
, '文件夾已經存在,程序繼續執行!!!')finally:with open('酷狗音樂\\' + song_name
+ '-' + author_name
+ '.mp3', 'wb') as f
:f
.write
(purl_data
)print(song_name
+ '-' + author_name
, '下載完成')time
.sleep
(0.2)except Exception
as e
:passcontinueelse:num
= int(input('需要下載的序號:'))hash_id_info
= list_hash
[int(num
-1)]album_id_info
= album_id_list
[int(num
-1)]print(hash_id_info
,album_id_info
)movie_url
= f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={hash_id_info}&dfid=33ZWG24MjQ2x0kYBM53k7N6C&mid=2070c26ef7eeac6d59cb3372ce068810&platid=4&album_id={album_id_info}&_=1606976867986'print(movie_url
)response
= requests
.get
(url
=movie_url
,headers
=headers
).textresponse_json
= json
.loads
(response
) print(response_json
)purl
= response_json
['data']['play_url']author_name
= response_json
['data']['author_name']song_name
= response_json
['data']['song_name']print(purl
)purl_data
= requests
.get
(url
=purl
,headers
=headers
).content
try:os
.mkdir
('酷狗音樂')except Exception
as e
:print(e
,'文件夾已經存在,程序繼續執行!!!')finally:with open('酷狗音樂\\'+song_name
+'-'+author_name
+'.mp3','wb') as f
:f
.write
(purl_data
)print(song_name
+'-'+author_name
,'下載完成')time
.sleep
(0.2)
else:print('找不到歌曲信息哦~~~')
總結
以上是生活随笔為你收集整理的酷狗音乐的爬虫练习的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。