介绍
这几天都在研究爬虫,然后捣鼓一下午写好了这个,可以爬取QQ付费音乐
(技术有限,只能爬取部分
)
有个bug,cookei过期,好像就用不了了
PS:需要导入两个模块
需在D盘新建music文件夹,不然报错,也可以自定义修改保存路径
快速导入模块方法如下:
win+r 输入cmd
在cmd里面分别复制粘贴回车进行了
pip install -i https://pypi.doubanio.com/simple/ requests
pip install -i https://pypi.doubanio.com/simple/ prettytable
效果
代码
import requests
import prettytable as pt #一个格式输出的模块
geshou = input('请输入你想要下载的歌手或者歌曲名:')
#输入歌手名字
url = f'http://u.y.qq.com/cgi-bin/musicu.fcg?data=%7B%22comm%22:%7B%22ct%22:%2219%22,%22cv%22:%221882%22,%22uin%22:%220%22%7D,%22searchMusic%22:%7B%22method%22:%22DoSearchForQQMusicDesktop%22,%22module%22:%22music.search.SearchCgiService%22,%22param%22:%7B%22grp%22:1,%22num_per_page%22:30,%22page_num%22:1,%22query%22:%22%7B{geshou}%7D%22,%22search_type%22:0%7D%7D%7D%0A'
#f可打印输出
data = requests.get(url).json()
#获取url json
zhi = data['searchMusic']['data']['body']['zhida']['list'][0]
#json取值
song = zhi['track_list']['items']
tb = pt.PrettyTable()
#打印表格
tb.field_names = ['序号','歌名','状态']
#打印表格
music_info_list = []
numbers = 0 #定义歌曲序号
for list in song: #for循环
id = list['mid'] #取歌曲id
name = list['name']#取歌曲昵称
zt = '√' # 状态
# print(numbers,name,zt) #测试输出
tb.add_row([numbers,name,zt]) #赋予表格内容
music_info_list.append([id,name,'下载成功'])
numbers += 1
print(tb) #输出表格样式
headers = {
headers = {
'cookie': 'pgv_pvid=408238320; fqm_pvqid=c12454a0-4580-4d49-b886-2bf22db76a9e; ts_uid=7308117800; RK=mnvFA8P8ep; ptcz=e886f2438004cd93a6548c522422af8d91ee8453d3fb8d0d838fe4e98863764e; euin=oKvsNKElNKEkon**; tmeLoginType=2; pac_uid=1_469979950; iip=0; o_cookie=1469979950; tvfe_boss_uuid=bf2125500525cd30; pgv_info=ssid=s8054603064; _qpsvr_localtk=0.3553167419354355; fqm_sessionid=fbb172c4-1e70-4c71-bed7-a7c049452ae6; ts_refer=i.y.qq.com/; ariaDefaultTheme=undefined; ts_last=y.qq.com/n/ryqq/search; login_type=1; qm_keyst=Q_H_L_5ScLucJrkG-lUI0lfjBuXm5idKRNPJQ5gOi6AQa-45tBhho-xvsERMA; wxopenid=; psrf_qqopenid=413A01BF0479B022D91B709C577DF361; psrf_qqaccess_token=ABFD4D3D17C22695A37D62587674B6EA; psrf_access_token_expiresAt=1684934769; qqmusic_key=Q_H_L_5ScLucJrkG-lUI0lfjBuXm5idKRNPJQ5gOi6AQa-45tBhho-xvsERMA; wxrefresh_token=; psrf_qqunionid=9EB7B4A00E86499307EADADC8B935FD1; qm_keyst=Q_H_L_5ScLucJrkG-lUI0lfjBuXm5idKRNPJQ5gOi6AQa-45tBhho-xvsERMA; uin=1469979950; psrf_musickey_createtime=1677158769; psrf_qqrefresh_token=C4FF1DABB43775D586C194BF1F17934D; wxunionid=',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.50'
}
#模拟请求
}
while True:
input_index = eval(input("请输入你要下载的歌曲序号(按-1退出):"))
#定义输出
if input_index == -1:
#输入-1退出输入
break #退出
download_info = music_info_list[input_index]
#取歌曲名称
mid = download_info[0]
#取歌曲mid
music_url = ('https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&data={"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"358840384","songmid":["%s"],"songtype":[0],"uin":"1443481947","loginflag":1,"platform":"20"}},"comm":{"uin":"18585073516","format":"json","ct":24,"cv":0}}'% mid)
#请求
purl = 'https://dl.stream.qqmusic.qq.com/' + requests.get(music_url,headers=headers).json()['req_0']['data']['midurlinfo'][0]['purl']
#取值分割
music_data = requests.get(purl,headers=headers).content
#get
with open(f'D/music/{download_info[1]}.mp3', mode= 'wb')as f:
#定义保存路径,歌曲名称
f.write(music_data)
#保存
print(f'{download_info[1]}','下载完成!')
print(purl)
#输出下载结果
评论 (0)