Python实现一个音乐下载器,不存在充会员的 | 附源码
halo,包子们上午好
很多学计算机的小伙伴应该都知道,毕业设计是一个头疼的东西
今天的话小编这边给大家用Python实现一个音乐下载器
这不是毕业设计必备项目
说实话操作起来还是有那么一点点的难度的,但是大家不用担心
作为一个宠粉狂魔的小编,肯定都给大家准备好了的
直接上才艺
关注小编,私信小编领取哟!
当然别忘了一件三连哟~~
公众号:Python日志
源码领取QQ群:494958217
Python版本:3.7.8
相关模块:
requests模块;
re模块;
os模块;
以及一些python自带的模块。
import os
import re
from urllib import parse
import requests
import sys
from PyQt5.QtGui import QIcon, QPixmap, QTextCursor
from PyQt5 import QtWidgets
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
from music import Ui_Form # 界面
headers = {
'Accept-Language': 'zh-CN,zh;q=0.9',
# 浏览器基本信息
'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
# 辨别用户的身份
'Cookie': '_ga=GA1.2.1046563906.1629973377; _gid=GA1.2.864288642.1637645958; _gat=1; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1635428199,1637645959,1637647069,1637650108; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1637650108; kw_token=79O0NFTF9MH',
# 认证令牌
'csrf': '79O0NFTF9MH',
# 指定的请求资源的域名
'Host': 'www.kuwo.cn'
}
class Pyqt5_Music(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super(Pyqt5_Music, self).__init__()
self.setupUi(self)
self.init()
self.setWindowTitle("音乐下载器")
self.setWindowIcon(QIcon('音乐_填充.svg'))
self.lineEdit.setText("")
self.lineEdit_2.setText("")
self.lineEdit_3.setText("")
pix = QPixmap('Python大本营.jpg')
self.label_6.setPixmap(pix)
self.label_6.setScaledContents(True)
def init(self):
self.pushButton.clicked.connect(self.song_ok)
self.pushButton_2.clicked.connect(self.main_exit)
self.pushButton_3.clicked.connect(self.show_child)
def song_ok(self):
music_info_list = self.get_music_info()
music_data = self.get_purl(music_info_list)
self.save_music_mp3(music_data)
def main_exit(self):
sys.exit(self)
def show_child(self):
QDesktopServices.openUrl(QUrl("https://ke.qq.com/course/package/51020?tuin=8ac397eb"))
def get_music_info(self):
"""搜索歌曲"""
music_info_list = []
name = self.lineEdit.text()
page = self.lineEdit_2.text()
num = self.lineEdit_3.text()
headers['Referer'] = 'http://www.kuwo.cn/search/list?key=' + parse.quote(name)
url = f'http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={name}&pn={page}&rn={num}'
response = requests.get(url, headers=headers)
music_data = response.json() # 获取到的是字符串
music_list = music_data['data']['list']
for music in music_list:
music_name = music['name'] # 歌曲的名字
singer_name = music['artist'] # 歌手的名字
songmid = music['rid']
media_mid = music['album']
music_info_list.append((music_name, singer_name, songmid, media_mid))
return music_info_list
def get_purl(self, a):
"""获取歌曲url"""
music_data = []
for music in a:
music_name = music[0]
singer_name = music[1]
songmid = music[2]
purl = f'https://www.kuwo.cn/api/v1/www/music/playUrl?mid={songmid}&type=convert_url3&br=320kmp3'
full_media_url = requests.get(purl, headers=headers).json()['data']['url']
music_name = re.sub(r'[\/:*?"<>|]', '', music_name)
music_data.append(
{
'music_name': music_name,
'singer_name': singer_name,
'full_media_url': full_media_url
}
)
return music_data
def save_music_mp3(self, b):
"""保存歌曲"""
if not os.path.exists('歌曲下载'): # 判断是否有歌曲下载文件夹
os.mkdir('歌曲下载') # 如果没有创建 歌曲下载文件夹
for music in b:
music_name = music['music_name']
singer_name = music['singer_name']
full_url = music['full_media_url']
music_response = requests.get(full_url).content
with open('歌曲下载/%s-%s.mp3' % (music_name, singer_name), 'wb')as fp:
fp.write(music_response)
print("[%s]保存成功!" % music_name)
self.textEdit.append("[%s-%s.mp3]保存成功!" % (music_name, singer_name))
cursor = self.textEdit.textCursor()
cursor.movePosition(QTextCursor.End) # 还可以有别的位置
self.textEdit.setTextCursor(cursor)
【领取方式】
QQ扫码添加
【依次发放,请勿重复添加】
添加时备注“公众号”
即可免费领取学习资料和课程
或者直接搜索QQ群:494958217 领取
评论