• 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('
', ' ').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()
  • 相关阅读:
    [C++]Linux之进程间通信小结【待完善】
    [转] thrift的使用介绍
    [转] splice系列系统调用
    [转] gdb中忽略信号处理
    [转] 确定性投资的框架
    [转] 投资策略及投资体系
    [转] 为什么医疗咨询服务公司Evolent Health仅用4年就华丽上市?
    [转] When exactly does the virtual table pointer (in C++) gets set for an object?
    [转] Linux写时拷贝技术(copy-on-write)
    [转] .bss段和.data段的区别
  • 原文地址:https://www.cnblogs.com/LQ970811/p/10493814.html
Copyright © 2020-2023  润新知