用户:Charlie查看:2 回复:3 评论:2 创建时间:2020-08-17T18:17:15
【作品展示】

【作品介绍】
利用网络爬虫,爬取酷狗音乐,免费下载,仅用于学习,不做商务用途。
【作品源代码】
# -*- coding:utf-8 -*-
'''
项目描述:用于抓取酷狗音乐,并下载到本地
声明:本程序仅用于学习,不用做商业用途
'''
import requests
import json
import re
x = input("请输入歌曲名字:")
url = "https://songsearch.kugou.com/song_search_v2?callback=jQuery112403350172950871444_1557912515873&keyword={}&" \
"page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2" \
"&iscorrection=1&privilege_filter=0&_=1557912515875".format(x)
res = requests.get(url).text
js = json.loads(res[res.index('(') + 1:-2])
data = js['data']['lists']
for i in range(10):
print(str(i + 1) + ">>>" + str(data[i]['FileName']).replace('', '').replace('', ''))
number = int(input("请输入要下载的歌曲序号:"))
name = str(data[number - 1]['FileName']).replace('', '').replace('', '')
fhash = re.findall('"FileHash":"(.*?)"', res)[number - 1]
hash_url = "https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191036570904451680586_1557914756367" \
"&hash={}&album_id=14445990&dfid=4DoNQ81bmUyd0D0Grp3NO2nG&" \
"mid=a1b0459c73168ee1697c9951514fb0d7&platid=4&=1557914756369".format(fhash)
hash_content = requests.get(hash_url)
play_url = ''.join(re.findall('"play_url":"(.*?)"', hash_content.text))
real_download_url = play_url.replace("\\", "")
with open(name + ".mp3", "wb")as fp:
fp.write(requests.get(real_download_url).content)
print("下载成功")
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn