日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

酷狗音乐的爬虫练习

發布時間:2023/12/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 酷狗音乐的爬虫练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

酷狗音樂的爬蟲練習(只是作為新手練習,大神勿噴啊)

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 # print(html_data) # print(len(html_data)) html_info = html_data[html_data.find('(')+1:-2] #不規則的dict類型。需要進行轉換 html_data_info = json.loads(html_info) #將str轉換為字典dict類型 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>','') #將<em>進行替換singerName = list['SingerName'].replace('<em>','').replace('</em>','')FileHash = list['FileHash']AlbumID = list['AlbumID']album_id_list.append(AlbumID)list_hash.append(FileHash) #提取FileHashrow = {'歌手':singerName,'歌名':SongName# 'hash':FileHash}list_id_info.append(row)print(list_hash) # print(list_id_info) a = 1 if len(list_id_info) >= 1:for name in list_id_info:print(a,name)a = a + 1#顯示歌曲列表信息 序號all = 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)# movie_url = f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={hash}&album_id=38606351&dfid=0zce6Z1T3Kjv19ea8L221lxk&mid=fe277423a92d0e4b3326e3a660cc8689&platid=4&_=1603503473577'response = requests.get(url=movie_url, headers=headers).textresponse_json = json.loads(response) # 將str轉換為字典dict類型# 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).contenttry: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'# movie_url = f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={hash_id_info}&album_id=38606351&dfid=0zce6Z1T3Kjv19ea8L221lxk&mid=fe277423a92d0e4b3326e3a660cc8689&platid=4&_=1606976867986'print(movie_url)response = requests.get(url=movie_url,headers=headers).textresponse_json = json.loads(response) #將str轉換為字典dict類型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).contenttry: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('找不到歌曲信息哦~~~')

總結

以上是生活随笔為你收集整理的酷狗音乐的爬虫练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。