Mrhe 发表于 2021-10-13 16:11

python爬虫酷狗排行

import requests
import re
import time


def Tools(url):
    headers = {
      "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" "AppleWebKit/537.36 (KHTML, like Gecko)" "Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38"
    }
    response = requests.get(url, headers=headers)
    return response

def Save(name,url):
    mp3 = Tools(url).content #请求MP3地址链接 返回格式16进制
    f = open("./kugou/{}.mp3".format(name),"wb")#w文件存在写入 不存在就创建 b进制读写
    f.write(mp3)
    f.close()
    print("{}下载完成.......".format(name))

url = 'https://www.kugou.com/yy/html/rank.html'
response = Tools(url).text
# print(response)
album_id = re.findall(r'"album_id":(.*?),', response)
Hash = re.findall(r'"Hash":"(.*?)",', response)
# print(album_id)
# print(Hash)
for a, h in zip(album_id, Hash):
    # 生成时间戳
    time1 = int(time.time() * 1000)
    # print(time1)
    # 包含下载歌曲地址URL
    urls = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={}&appid=1014&mid=5466a3fe42668debc4a044c5ba28dd2a&platid=4&album_id={}&_={}'.format(
      h, a, time1)
    # print(urls)
    response1 = Tools(urls).json()
    #print(response1)
    audio_name = response1["data"]["audio_name"].split('-')[1]
    play_url = response1["data"]["play_url"]
    #print(audio_name,play_url)
    Save(audio_name,play_url)

lgc007 发表于 2021-10-13 18:44

涨知识,涨知识

乖、表闹 发表于 2021-10-13 19:06

def Save(name,url):
    mp3 = Tools(url).content #请求MP3地址链接 返回格式16进制
    with open(f"./kugou/{name}.mp3","wb") as f:
      f.write(mp3)
      print(f"{name}下载完成.......")

小小的石头13 发表于 2021-10-13 21:08

学习到了

西岭千秋雪 发表于 2021-10-14 13:41

涨姿势涨姿势

kci23 发表于 2021-10-14 15:05

学到了 谢谢分享

414246704 发表于 2021-10-20 09:33

发生错误用不了 ,本来是很需要这个狗酷的代码。
页: [1]
查看完整版本: python爬虫酷狗排行