爬虫案例-爬取某狗音乐

文章目录

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、效果图


相关推荐
Smartdaili China2 小时前
OpenClaw赋能AI智能体:实时联网与网页抓取
人工智能·爬虫·ai·爬取·openclaw·open claw
IP搭子来一个7 小时前
爬虫使用代理 IP 频繁失效,该如何定位问题?
网络·爬虫·tcp/ip
weixin_4684668512 小时前
Crawl4Ai 智能数据采集与场景化应用指南
大数据·人工智能·爬虫·python·数据分析
小熊Coding1 天前
Python爬取当当网二手图书项目实战!
开发语言·爬虫·python·beautifulsoup·requests·二手图书
IP搭子来一个1 天前
爬虫采集大量返回 403、429,到底卡在哪一环?
网络·爬虫·python
小白学大数据1 天前
Playwright 爬虫:Python 爬取 JS 渲染的 JSP 网站
开发语言·javascript·爬虫·python·数据分析
遇事不決洛必達1 天前
【爬虫随笔】常见加密算法特征总结
javascript·爬虫·逆向·加密算法
小白学大数据1 天前
电商关键词挖掘:Java 爬虫抓取 1688 推荐搜索词
java·开发语言·爬虫·python
小白学大数据1 天前
浅析爬虫技术更迭:静态请求与浏览器渲染采集能力对比
爬虫·python·spring·数据分析
2601_957888561 天前
流量终局与信源争夺:GEO(生成式引擎优化)时代的爬虫分析与数据管道构建
人工智能·爬虫