Python教程

python爬取网易云音乐飙升榜

本文主要是介绍python爬取网易云音乐飙升榜,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import time
from multiprocessing.dummy import Pool
import requests
import os

filename = "网易云飙升榜//"
if not os.path.exists(filename):
    os.mkdir(filename)

url = "https://music.163.com/weapi/v6/playlist/detail?csrf_token="
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
}
params = {
    "params": "5fFJz3vX8o556VRwH1lQ1VOPliD5GUpmLB0gF92Q4bRf4q6wXyfj9/4BQ8DJMwvDErkW4wg7lfU+9wMLRL8J/pQpgFWbMCRpGBAmLHAmiRA=",
    "encSecKey": "571762ea14eec58ed6b4e2f230a031e2c777eb0c37fe9f1e3727d19910660c138be02fa469da2d5c139a86f59da13fad1bac3e065d01eaed594a6a575d61189c4db2e0d1b3bf1f0d10ac86a2fbad554f82a9722a22066546b2dbd356ea4e612649ceb79560004363b531bbf43344617bd204fe50362ade347c37a0b9008383de"
}
resp = requests.post(url=url, headers=headers, params=params).json()

dic = {}
result = []
for music_info in resp["playlist"]["tracks"]:
    music_url = f'http://music.163.com/song/media/outer/url?id={music_info["id"]}.mp3'

    dic = {
        'url': music_url,
        'name': music_info["name"]
    }
    result.append(dic)

start_time = time.time()
def musicDownload(dic):
    print("正在下载-"+dic['name'])
    music_file = requests.get(url=dic["url"], headers=headers).content
    with open(filename + dic["name"] + ".mp3", "wb") as fp:
        fp.write(music_file)
pool = Pool(20)
pool.map(musicDownload,result)

end_time=time.time()
time1=end_time-start_time
print(f"共耗时:{time1}")
这篇关于python爬取网易云音乐飙升榜的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!