Python 酷狗音乐数据爬取:GUI 应用程序实现
Python 酷狗音乐数据爬取:GUI 应用程序实现
本教程将教你如何使用 Python 编写一个 GUI 应用程序,实现酷狗音乐网站数据的爬取,包括获取歌词、生成词云和保存到 Excel 表格等功能。
主要任务:
设计一个窗体应用系统,具有以下功能:
- 加载需要用到的各种第三方库,如 requests; BeautifulSoup4; lxml; sqlite3; jieba;; WordCloud; openpyxl 等。
- 将信息保存到 Excel 表中。
- 显示处理后的信息内容:由于涉及到 GUI 界面的设计,建议使用 PyQt 或 Tkinter 等 GUI 库来实现。
以下是一个使用 PyQt 实现的示例代码:
import sys
import requests
from bs4 import BeautifulSoup
import lxml
import sqlite3
import jieba
from wordcloud import WordCloud
import openpyxl
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QTextEdit, QFileDialog, QMessageBox
from PyQt5.QtGui import QIcon
class KugouSpider(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置窗口标题和大小
self.setWindowTitle('酷狗音乐网站数据爬取')
self.setFixedSize(500, 600)
self.setWindowIcon(QIcon('icon.png'))
# 创建控件
self.label1 = QLabel('歌手名:', self)
self.label1.move(50, 50)
self.lineEdit1 = QLineEdit(self)
self.lineEdit1.move(120, 50)
self.label2 = QLabel('歌曲名:', self)
self.label2.move(50, 100)
self.lineEdit2 = QLineEdit(self)
self.lineEdit2.move(120, 100)
self.label3 = QLabel('歌词:', self)
self.label3.move(50, 150)
self.textEdit1 = QTextEdit(self)
self.textEdit1.move(120, 150)
self.label4 = QLabel('词云图:', self)
self.label4.move(50, 350)
self.textEdit2 = QTextEdit(self)
self.textEdit2.move(120, 350)
self.button1 = QPushButton('获取歌词', self)
self.button1.move(350, 50)
self.button2 = QPushButton('生成词云', self)
self.button2.move(350, 100)
self.button3 = QPushButton('保存到Excel', self)
self.button3.move(350, 150)
# 绑定事件
self.button1.clicked.connect(self.get_lyric)
self.button2.clicked.connect(self.generate_wordcloud)
self.button3.clicked.connect(self.save_to_excel)
def get_lyric(self):
# 获取歌曲信息
singer = self.lineEdit1.text()
song = self.lineEdit2.text()
# 发送请求
url = f'http://www.kugou.com/yy/html/search.html#searchType=song&searchKeyWord={song}&page=1'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'lxml')
# 获取歌曲链接
song_url = soup.select('.song_name > a')[0]['href']
# 发送请求
response = requests.get(song_url, headers=headers)
soup = BeautifulSoup(response.text, 'lxml')
# 获取歌词
lyric = soup.select('.lrc_content')[0].get_text()
# 显示歌词
self.textEdit1.setText(lyric)
def generate_wordcloud(self):
# 获取歌词
lyric = self.textEdit1.toPlainText()
# 分词
words = jieba.cut(lyric)
# 生成词云
wordcloud = WordCloud(font_path='msyh.ttc', width=600, height=400).generate(' '.join(words))
# 显示词云
self.textEdit2.setPlainText('')
self.textEdit2.setHtml(f'<img src='{wordcloud.to_image()}'/>')
def save_to_excel(self):
# 获取歌曲信息
singer = self.lineEdit1.text()
song = self.lineEdit2.text()
lyric = self.textEdit1.toPlainText()
# 保存到Excel
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = '歌曲信息'
sheet['A1'] = '歌手'
sheet['B1'] = '歌曲'
sheet['C1'] = '歌词'
sheet['A2'] = singer
sheet['B2'] = song
sheet['C2'] = lyric
filename, _ = QFileDialog.getSaveFileName(self, '保存到Excel', '', 'Excel文件 (*.xlsx)')
if filename:
wb.save(filename)
QMessageBox.information(self, '提示', '保存成功!')
if __name__ == '__main__':
app = QApplication(sys.argv)
spider = KugouSpider()
spider.show()
sys.exit(app.exec_())
在代码中,我们使用 PyQt5 库实现了一个窗口应用程序,包含了获取歌词、生成词云和保存到 Excel 等功能。具体实现过程如下:
- 创建窗口和各种控件,包括标签、文本框、按钮等。
- 绑定按钮的点击事件,分别实现获取歌词、生成词云和保存到 Excel 等功能。
- 在获取歌词功能中,我们首先获取用户输入的歌曲名,然后发送请求到酷狗音乐网站,并使用 BeautifulSoup 解析网页内容,获取歌曲链接和歌词。最后将歌词显示在文本框中。
- 在生成词云功能中,我们首先获取文本框中的歌词,然后使用 jieba 库进行分词,再使用 WordCloud 库生成词云图。最后将词云图显示在文本框中。
- 在保存到 Excel 功能中,我们首先获取用户输入的歌曲名、歌手和歌词,然后使用 openpyxl 库创建 Excel 表格,并将歌曲信息保存到表格中。最后弹出保存文件对话框,让用户选择保存路径和文件名。
注意:在使用 PyQt5 库时,需要先安装相应的库文件,可以使用 pip 命令进行安装。如果程序无法正常运行,可以尝试更新或重新安装 PyQt5 库。
原文地址: https://www.cveoy.top/t/topic/f1dB 著作权归作者所有。请勿转载和采集!