python带你制作一个歌曲下载器,海量歌曲免费听

前言

大家早好、午好、晚好吖~


环境使用:

  • Python 3.8

  • Pycharm


安装python第三方模块:

  1. win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车

  2. 在pycharm中点击Terminal(终端) 输入安装命令


如何配置pycharm里面的python解释器?

  1. 选择file(文件) >>> setting(设置) >>> Project(项目) >>> python interpreter(python解释器)

  2. 点击齿轮, 选择add

  3. 添加python安装路径


pycharm如何安装插件?

  1. 选择file(文件) >>> setting(设置) >>> Plugins(插件)

  2. 点击 Marketplace 输入想要安装的插件名字 比如:翻译插件 输入 translation / 汉化插件 输入 Chinese

  3. 选择相应的插件点击 install(安装) 即可

  4. 安装成功之后 是会弹出 重启pycharm的选项 点击确定, 重启即可生效


代码

导入模块

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',

网站名得链接发出来就不给过拉~宝子们自己按照下面得图添加一下哦

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('清风微信.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 get_music_info(self):
        music_info_list = []
        name = self.lineEdit.text()
        page = self.lineEdit_2.text()
        num = self.lineEdit_3.text()
        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

获取歌曲url

    def get_purl(self, a):
        music_data = []
        for music in a:
            music_name = music[0]
            singer_name = music[1]
            songmid = music[2]
            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)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    my_show = Pyqt5_Music()
    my_show.show()
    sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(724, 328)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(80, 100, 101, 16))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(80, 130, 101, 16))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(80, 160, 101, 16))
        self.label_3.setObjectName("label_3")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(190, 100, 201, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(Form)
        self.lineEdit_2.setGeometry(QtCore.QRect(190, 130, 201, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(Form)
        self.lineEdit_3.setGeometry(QtCore.QRect(190, 160, 201, 20))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.label_4 = QtWidgets.QLabel(Form)
        self.label_4.setGeometry(QtCore.QRect(140, 30, 151, 20))
        font = QtGui.QFont()
        font.setFamily("幼圆")
        font.setPointSize(14)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(Form)
        self.label_5.setGeometry(QtCore.QRect(240, 70, 101, 16))
        self.label_5.setObjectName("label_5")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(90, 290, 81, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(234, 290, 81, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.textEdit = QtWidgets.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(60, 190, 361, 91))
        self.textEdit.setObjectName("textEdit")
        self.label_6 = QtWidgets.QLabel(Form)
        self.label_6.setGeometry(QtCore.QRect(470, 70, 200, 200))
        self.label_6.setText("")
        self.label_6.setObjectName("label_6")
        self.pushButton_3 = QtWidgets.QPushButton(Form)
        self.pushButton_3.setGeometry(QtCore.QRect(530, 290, 111, 23))
        self.pushButton_3.setObjectName("pushButton_3")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "请输入歌手/歌名:"))
        self.label_2.setText(_translate("Form", "下载页数:"))
        self.label_3.setText(_translate("Form", "歌曲数量:"))
        self.label_4.setText(_translate("Form", "音乐下载器"))
        self.label_5.setText(_translate("Form", "作者:青灯教育"))
        self.pushButton.setText(_translate("Form", "确定"))
        self.pushButton_2.setText(_translate("Form", "关闭"))
        self.pushButton_3.setText(_translate("Form", "系统学习,请扫码!"))

效果

尾语 💝

好了,我的这篇文章写到这里就结束啦!

有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

版权声明:
作者:admin
链接:https://www.techfm.club/p/45371.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>