QQ音乐

import re

import requests
import json


class Search:
def __init__(self, song):
'''
self.vkey_url ---> 获取vkey的url
self.search_url ---> 获取歌曲列表的url
self.lyric_url ---> 获取歌词的url
self.music_url ---> 歌曲url的头,加上获得的vkey就是歌曲url
:param song: 歌曲名或歌手
'''
self.vkey_url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?callback=getplaysongvkey626277975566637&g_tk=5381&jsonpCallback=getplaysongvkey626277975566637&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0&data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"552068528","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"552068528","songmid":["%s"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":20,"cv":0}}'

self.search_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=69457515379027970&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=20&w={}&g_tk=5381&jsonpCallback=MusicJsonCallback010045556336209849&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0'.format(
song)

self.lyric_url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric.fcg?nobase64=1&musicid=%s&callback=jsonp1&g_tk=5381&jsonpCallback=jsonp1&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
}
self.music_url = 'http://183.230.77.24/amobile.music.tc.qq.com/%s'
self.seesion = requests.Session()
self.seesion.headers = headers

def get_song_list(self):
'''
搜索到的歌曲列表
默认第一首歌曲,其他的更改下标[0]
:return:
'''
res = self.seesion.get(self.search_url)
song_list_json = json.loads(res.text.strip('MusicJsonCallback010045556336209849(').strip(')'))
# 只看第一首,修改下标[0],查看其它
songmid = song_list_json['data']['song']['list'][0]['mid']
# songmid = song_list_json['data']['song']['list'][0]['file']['media_mid']
songid = song_list_json['data']['song']['list'][0]['id']
self.songname = song_list_json['data']['song']['list'][0]['title']
print(songmid, songid, self.songname)
self.get_msuic_vkey(songmid, songid)

def get_msuic_vkey(self, mid, songid):
'''
获得vkey
:param mid: 歌曲的mid
:param songid: 歌曲的id
:return:
'''
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'referer': 'https://y.qq.com/portal/player.html',
}
res = self.seesion.get(self.vkey_url % mid, headers=header)
res = json.loads(res.text.strip('getplaysongvkey626277975566637(').strip(')'))
purl_vkey = res['req_0']['data']['midurlinfo'][0]['purl']
self.get_msuic(purl_vkey)
self.get_lyric(songid, mid)

def get_msuic(self, purl):
with open(self.songname + '.mp3', 'wb') as f:
f.write(self.seesion.get(self.music_url % purl).content)

def get_lyric(self, id, mid):
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'referer': 'https://y.qq.com/n/yqq/song/{}.html'.format(mid)
}
lyric = self.seesion.get(self.lyric_url % id, headers=header).text
lyric = re.search('"lyric":"(.*?)"', lyric).group(1)
lyric = self.del_not_need(lyric)
with open(self.songname + '.lrc', 'w') as f:
f.write(lyric)

# def change_symbol(self, lrc):
def del_not_need(self, lrc):
'''
更改歌词中的HTML实体字符
:param lrc: 歌词
:return:
'''
lyric = lrc.replace(':', ':').replace('
', '\n').replace('.', '.').replace('-', '-')
lyric = lyric.replace(' ', ' ').replace('(', '(').replace(')', ')').replace('/', '/')
lyric = lyric.replace(',', ',').replace('|', '|').replace('*', '*')
return lyric

def run(self):
'''
程序运行入口
:return:
'''
self.get_song_list()


if __name__ == '__main__':
a = Search('真的好想你')
a.run()
上一篇:30行代码爬取网易云歌词


下一篇:手把手教你使用Python抓取QQ音乐数据!