马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 aa1448176630 于 2020-6-1 15:34 编辑 import requests as r
import json
import re
import os
head={'User-Agent':'Mozilla/5.0'}
music_list=[]#歌曲链接
sname_list=[]#歌手名
name_list=[]#歌名
#m2_list=p['data']['song']['list'][i]['file']['media_mid']#歌曲mid
def find(m_list):
for each in m_list:
#print(each['mid'])#与media_mid相同都是歌曲mid
name_list.append(each['name'])#添加歌名
mid_list=each['file']['media_mid']#获取mid
sname_list.append(each['singer'][0]['name'])#添加歌手名
#print(name_list)
#print(each['file']['media_mid'])
new_url='https://y.qq.com/n/yqq/song'+'//'+mid_list+'.html'
#print(new_url)
data={'mid': new_url}
req=r.post('http://www.douqq.com/qqmusic/qqapi.php',headers=head,data=data).text
req=json.loads(req)#json解析完后还是string 并非字典 所以下面用正则获取链接
req=req.replace('\/','/')
p=r'm4a":"([^"]+)'
nl=re.findall(p,req)
music_list.append(nl)#添加歌曲链接
def document():
i=0
if not os.path.exists(r'C:\Users\Administrator\Desktop\music'):
os.mkdir(r'C:\Users\Administrator\Desktop\music')
os.chdir(r'C:\Users\Administrator\Desktop\music')
for each in music_list:
#print(each)
if not each:
i=i+1
continue
path=r'C:\Users\Administrator\Desktop\music'+'\\'+name_list[i]+','+sname_list[i]+'.mp3'
with open(path,'wb+') as f:
#print("1")
#print(music_list[i][0],name_list[i])
p=r.get(music_list[i][0]).content
f.write(p)
print(name_list[i]+','+sname_list[i]+"下载完成")
i=i+1
if __name__=='__main__':
try:
content=input("请输入歌手名或歌名:")
#url由抓包所得(抓搜索音乐时的url)
url='https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=55043609442258168&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=10&w={}&g_tk_new_20200303=5381&g_tk=5381&loginUin=229554158&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0'.format(content)
res=r.get(url,headers=head)
p=json.loads(res.text)
m_list=p['data']['song']['list']#获取歌曲列表
except:
print("发生错误!!")
find(m_list)
document()
爬虫新手 只是为交作业 大佬勿喷 |