当前位置:   article > 正文

python 爬取音乐_python爬取音乐

python爬取音乐
# pip install requests 安装请求模块
import requests  # 发送请求模块
import json # json 解析模块 不需要安装系统自动

# 服务器响应的结束数据的格式
# (1) .text 代表访问的数据是文字
# (2) .content 代表访问的数据是多媒体文件(图片, 音乐, 视频, 文件)
# (3) .json 访问的文字是 json 类型

# 设置请求头
headers = {
    "csrf": "T8NAL6H9FNT",
    "Cookie": 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1668849408; _ga=GA1.2.1770505991.1668849408; _gid=GA1.2.1722579019.1668849408; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1668863105; kw_token=T8NAL6H9FNT',
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

# 请求地址
pid_url = "https://kuwo.cn/api/www/bang/bang/musicList?bangId=93&pn=1&rn=30&httpsStatus=1&reqId=69e57dd0-680b-11ed-b8cf-23e9fab9a411"

# 发送 get 请求获取资源数据
pid_list = requests.get(pid_url, headers=headers)

# json.loads() 把字符串转 json
song_lists = json.loads(pid_list.text)['data']["musicList"]

# item.get() 获取对应对象的值
for index, item in enumerate(song_lists):
    print(f"{index + 1}---{item.get('artist')}---{item.get('name')}----{item.get('rid')}")

num = int(input("请输入第几首音乐:"))

header1 = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'Connection': 'keep-alive',
    'Host': 'kuwo.cn',
    'Referer': 'https://kuwo.cn/rankList',
    'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    "Cookie": 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1668849408; _ga=GA1.2.1770505991.1668849408; _gid=GA1.2.1722579019.16688"49408; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1668863375; kw_token=Y4XHS5XWSI',
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

# 添加请求头
headers2 = {
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
}

while num >= 0 & num <= len(song_lists) - 1:
    song_num_item = song_lists[int(num)]
    song_rid = song_num_item.get('rid')
    song_name = f"{song_num_item.get('name')}_{song_num_item.get('artist')}"
    song_url = f"https://kuwo.cn/api/v1/www/music/playUrl?mid={song_rid}"

    song_item_lists = requests.get(song_url, headers=header1)

    song_item = json.loads(song_item_lists.text)
    song_item_success = song_item.get("success")

    song_item_url = ''
    if song_item_success:
        song_item_url = song_item.get("data").get('url')
        song_content = requests.get(song_item_url, headers=headers2)
        with open(f"{song_name}.mp3", 'wb') as f:
            f.write(song_content.content)
            print("下载完成")
            break
    else:
        print("音乐无法下载")
        num = int(input("请输入第几首音乐:"))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/862564
推荐阅读
相关标签
  

闽ICP备14008679号