吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 34845|回复: 145
收起左侧

[Python 转载] Python 收费、高品质无损音乐下载【开源】

    [复制链接]
珍珠奶茶丶板牙 发表于 2018-7-20 18:21
本帖最后由 珍珠奶茶丶板牙 于 2018-7-21 16:55 编辑

下午闲着无聊,看到了@丸子吃枣药丸 他发的文章, API采用QQMusic,我将Python 实现了他的功能,更方便大家下载
QQ截图20180720181904.png
[Python] 纯文本查看 复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# [url=home.php?mod=space&uid=238618]@Time[/url]    : 2018/7/20 14:37
# [url=home.php?mod=space&uid=686208]@AuThor[/url]  : Bnightning
# [url=home.php?mod=space&uid=406162]@site[/url]    : https://www.bnightning.cn
# [url=home.php?mod=space&uid=267492]@file[/url]    : douqq.py
# @Software: PyCharm
import requests
import json

headers = {
    'Host': 'c.y.qq.com',
    'Referer': 'http://c.y.qq.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 '
                  'Safari/537.36 '
}


def douqq_post(mid):
    """
    返回歌曲下载url
    :param mid:歌曲mid
    :return: 字典
    """
    post_url = 'http://www.douqq.com/qqmusic/qqapi.php'
    data = {'mid': mid}
    res = requests.post(post_url, data=data)
    get_json = json.loads(res.text)
    return eval(get_json)


def download_file(src, file_path):
    """
    歌曲下载
    :param src: 下载链接
    :param file_path: 存储路径
    :return: 文件路径
    """
    r = requests.get(src, stream=True)
    f = open(file_path, "wb")
    for chunk in r.iter_content(chunk_size=512):
        if chunk:
            f.write(chunk)
    return file_path


def choice_download(dic):
    print('1. m4a视频')
    print('2. mp3普通品质')
    print('3. mp3高品质')
    print('4. ape高品无损')
    print('5. flac无损音频')
    select = int(input("Please input your choice:"))
    src = ''
    postfix = ''
    if select == 1:
        src = dic['m4a']
        postfix = '.m4a'
    if select == 2:
        src = dic['mp3_l']
        postfix = '.mp3'
    if select == 3:
        src = dic['mp3_h']
        postfix = '.mp3'
    if select == 4:
        src = dic['ape']
        postfix = '.ape'
    if select == 5:
        src = dic['flac']
        postfix = '.flac'
    return postfix, src.replace('\/\/', '//').replace('\/', '/')


def find_song(word):
    """
    查找歌曲
    :param word: 歌曲名
    :return: 返回歌曲mid
    """
    get_url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n' \
              '=20&w=' + word
    res1 = requests.get(get_url, headers=headers)
    get_json = json.loads(res1.text.strip('callback()[]'))
    jsons = get_json['data']['song']['list']
    songmid = []
    media_mid = []
    song_singer = []
    i = 1
    for song in jsons:
        # print(i, ':' + song['songname'], '---', song['singer'][0]['name'], song['songmid'], song['media_mid'])
        print(i, ':' + song['songname'], '---', song['singer'][0]['name'])
        songmid.append(song['songmid'])
        media_mid.append(song['media_mid'])
        song_singer.append(song['singer'][0]['name'])
        i = i + 1
    select = int(input("Please input your choice:")) - 1
    return songmid[select], song_singer[select]


if __name__ == '__main__':
    # songname = '叹云兮'
    songname = input("Please input the music name:")
    song_mid, singer = find_song(songname)
    dic = douqq_post(song_mid)
    # {
    # "mid":"004FjJo32TISsY",
    # "m4a":"http:\/\/dl.stream.qqmusic.qq.com\/C400004FjJo32TISsY.m4a?guid=2095717240&vkey=0B599CA74745F8A27A33A1FED2C7F6925FFFE8ED040569FB3540EB011FE9C5A3D7F36EAE4BDBD450F25076A23EBAF95A5ECB54B22C5E8F10&uin=0&fromtag=38",
    # "mp3_l":"http:\/\/dl.stream.qqmusic.qq.com\/M500004FjJo32TISsY.mp3?guid=2095717240&vkey=0B599CA74745F8A27A33A1FED2C7F6925FFFE8ED040569FB3540EB011FE9C5A3D7F36EAE4BDBD450F25076A23EBAF95A5ECB54B22C5E8F10&uin=0&fromtag=53",
    # "mp3_h":"http:\/\/dl.stream.qqmusic.qq.com\/M800004FjJo32TISsY.mp3?guid=2095717240&vkey=0B599CA74745F8A27A33A1FED2C7F6925FFFE8ED040569FB3540EB011FE9C5A3D7F36EAE4BDBD450F25076A23EBAF95A5ECB54B22C5E8F10&uin=0&fromtag=53",
    # "ape":"http:\/\/dl.stream.qqmusic.qq.com\/A000004FjJo32TISsY.ape?guid=2095717240&vkey=0B599CA74745F8A27A33A1FED2C7F6925FFFE8ED040569FB3540EB011FE9C5A3D7F36EAE4BDBD450F25076A23EBAF95A5ECB54B22C5E8F10&uin=0&fromtag=53",
    # "flac":"http:\/\/dl.stream.qqmusic.qq.com\/F000004FjJo32TISsY.flac?guid=2095717240&vkey=0B599CA74745F8A27A33A1FED2C7F6925FFFE8ED040569FB3540EB011FE9C5A3D7F36EAE4BDBD450F25076A23EBAF95A5ECB54B22C5E8F10&uin=0&fromtag=53",
    # "pic":"https:\/\/y.gtimg.cn\/music\/photo_new\/T002R300x300M000003NZyTh4eMMsp.jpg?max_age=2592000"
    # }
    # print('mid:'+dic['mid'])
    postfix, url = choice_download(dic)
    save_path = "H:\\Music\\"
    download_file(url, save_path + songname + ' - ' + singer + postfix)
    print('Download Successful')

免费评分

参与人数 18吾爱币 +17 热心值 +14 收起 理由
mink0325 + 1 谢谢@Thanks!
41_ + 1 + 1 我很赞同!
losingstars + 1 + 1 最后的if改成while,就不会下载完一个就退出了。
dzcgl + 1 热心回复!
FENGMUTIAN + 1 + 1 谢谢@Thanks!
波波波波 + 1 + 1 <font style="vertical-align: inherit;"><font style=
abzy01 + 1 + 1 谢谢@Thanks!
最凉丶不过人心 + 1 用心讨论,共获提升!
iwzw + 1 + 1 我很赞同!
actionser + 1 + 1 用心讨论,共获提升!
kk1212 + 1 + 1 大神真厉害,能把想法变成现实。
0001 + 1 热心回复!
高冷粉条面 + 1 + 1 谢谢@Thanks!
枫叶之秋 + 1 + 1 热心回复!
czxtzh + 1 + 1 谢谢@Thanks!
Thending + 1 + 1 帮你们打包好了自取 https://www.lanzous.com/i1ga6wf
雪莱鸟 + 1 + 1 有些牛逼
丸子吃枣药丸 + 1 太优秀了吧

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

你的明知还未知 发表于 2018-7-20 18:38
对我来说  是天文
秋枫华年 发表于 2018-7-20 18:31
无言的肖邦 发表于 2018-8-18 11:34
Please input your choice:2
Traceback (most recent call last):

  File "<ipython-input-6-34504bf39787>", line 1, in <module>
    runfile('C:/Users/zhangjuntao/Desktop/musicdownload1/musicdownloads.py', wdir='C:/Users/zhangjuntao/Desktop/musicdownload1')

  File "D:\Anaconda\envs\python3.6\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 678, in runfile
    execfile(filename, namespace)

  File "D:\Anaconda\envs\python3.6\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 106, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/zhangjuntao/Desktop/musicdownload1/musicdownloads.py", line 116, in <module>
    download_file(url, save_path + songname + ' - ' + singer + postfix)

  File "C:/Users/zhangjuntao/Desktop/musicdownload1/musicdownloads.py", line 39, in download_file
    r = requests.get(src, stream=True)

  File "D:\Anaconda\envs\python3.6\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)

  File "D:\Anaconda\envs\python3.6\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)

  File "D:\Anaconda\envs\python3.6\lib\site-packages\requests\sessions.py", line 498, in request
    prep = self.prepare_request(req)

  File "D:\Anaconda\envs\python3.6\lib\site-packages\requests\sessions.py", line 441, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),

  File "D:\Anaconda\envs\python3.6\lib\site-packages\requests\models.py", line 309, in prepare
    self.prepare_url(url, params)

  File "D:\Anaconda\envs\python3.6\lib\site-packages\requests\models.py", line 383, in prepare_url
    raise MissingSchema(error)

MissingSchema: Invalid URL '': No schema supplied. Perhaps you meant http://?
头像被屏蔽
sy727272 发表于 2018-7-20 18:46
提示: 作者被禁止或删除 内容自动屏蔽
浙江-杺庝 发表于 2018-7-20 18:52
http://www.douqq.com/qqmusic  
我好奇他网站里面的东东
z327774948 发表于 2018-7-20 19:22
复制下去制表符都变了
shuiba3939 发表于 2018-7-20 19:28
可惜有源码我也不会用
YangHaishuai 发表于 2018-7-20 19:55
不会用~~
雪莱鸟 发表于 2018-7-20 20:05
不会使用的,请安装notepad++,有些制表符会有变的,那个路径是笔者的,自己变一下,python也要安装一下
gxj000 发表于 2018-7-20 20:12
谢谢分享!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-16 20:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表