爬虫案例-爬取某狗音乐

文章目录

1、爬取代码

python 复制代码
import time
import requests
import hashlib
import jsonpath
import os

url = "https://wwwapi.kugou.com/play/songinfo"

#伪造请求头
header= {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 QuarkPC/2.3.0.256"

}
#uuid = str(int(time.time()))
clinenttime = str(int(time.time()*1000))

#print(uuid)
#print(clinenttime)
# 数据列表
data_list = [
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
"appid=1014",
f"clienttime={clinenttime}",
"clientver=20000",
"dfid=2UHWk60sDseo11EBHT1lYwCJ",
"encode_album_audio_id=c2bdq0fc",
"mid=51d94f366e27b4cd47c2f46aa11ac7e6",
"platid=4",
"srcappid=2919",
"token=a062aa8f897a25ed7b96c1e3919c42febd73e21a1a775fafd959f2ff63e36697",
"userid=2307953011",
"uuid=51d94f366e27b4cd47c2f46aa11ac7e6",
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
]

# 将列表拼接成一个完整的字符串
data = "".join(data_list)
#MD5加密
md5_hash = hashlib.md5(data.encode()).hexdigest()
#md5 = md5_hash.upper()
#print(md5_hash)


params= {
    "srcappid":"2919",
    "clientver":"20000",
    "clienttime":{clinenttime},
    "mid":"51d94f366e27b4cd47c2f46aa11ac7e6",
    "uuid":"51d94f366e27b4cd47c2f46aa11ac7e6",
    "dfid":"2UHWk60sDseo11EBHT1lYwCJ",
    "appid":"1014",
    "platid":"4",
    "encode_album_audio_id":"c2bdq0fc",
    "token":"a062aa8f897a25ed7b96c1e3919c42febd73e21a1a775fafd959f2ff63e36697",
    "userid":"2307953011",
    "signature":{md5_hash}
}
#请求URL
request = requests.get(url,headers=header,params=params)
json = request.json()
#print(json)

#提取音乐名称、下载音乐地址
muisc_info = {
        "音乐名称": jsonpath.jsonpath(json,"$..audio_name")[0],
        "下载音乐地址" :  jsonpath.jsonpath(json,"$..play_url")[0]
}
print(muisc_info['音乐名称'],muisc_info['下载音乐地址'])
# 创建输出目录
output_folder = r"C:/Users/zzx/Desktop/音乐" + "/"
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# 下载音乐文件
music_url = muisc_info["下载音乐地址"]
music_response = requests.get(music_url, headers=header)

# 保存音乐文件
music_filename = os.path.join(output_folder, f"{muisc_info['音乐名称']}.mp3")
with open(music_filename, mode="wb") as f:
    f.write(music_response.content)

print(f"音乐已保存到: {music_filename}")

2、效果图


相关推荐
深蓝电商API8 小时前
爬虫日志分析:快速定位被封原因
爬虫·python
是Dream呀9 小时前
自动化打造信息影响力:用 Web Unlocker 和 n8n 打造你的自动化资讯系统
运维·前端·爬虫·自动化
喵手12 小时前
Python爬虫实战:研究生招生简章智能采集系统 - 破解考研信息不对称的技术方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集研究生招生简章·考研信息不对称·采集考研信息数据csv导出
喵手13 小时前
Python爬虫实战:构建全球节假日数据库 - requests+lxml 实战时区节假日网站采集(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·构建全球节假日数据库·采集时区节假日数据·采集节假日sqlite存储
静谧空间13 小时前
linux安装Squid
linux·运维·爬虫
喵手13 小时前
Python爬虫实战:招聘会参会企业数据采集实战 - 分页抓取、去重与增量更新完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·增量·零基础python爬虫教学·招聘会参会企业数据采集·分页抓取去重
喵手14 小时前
Python爬虫实战:医院科室排班智能采集系统 - 从零构建合规且高效的医疗信息爬虫(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·医院科室排版智能采集系统·采集医疗信息·采集医疗信息sqlite存储
喵手14 小时前
Python爬虫实战:实现 Playwright 的动态名言“瀑布流”采集器,采集名言内容、作者及出处等信息(附 JSON 格式数据导出)!
爬虫·python·爬虫实战·playwright·零基础python爬虫教学·构建动态名言瀑布流采集器·采集数据json导出
喵手14 小时前
Python爬虫实战:全国旅游景区名录智能采集系统 - 构建文旅大数据的基石(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·全国旅游景区名采集系统·文旅大数据·采集旅游景区sqlite存储
J_bean16 小时前
AI 智能爬虫实战
爬虫·ai·大模型