以下是实现以上功能的Python代码:

import requests
from bs4 import BeautifulSoup
import sqlite3
import jieba
from wordcloud import WordCloud
import openpyxl
from tkinter import *
from tkinter import messagebox

# 创建窗体
root = Tk()
root.title("酷狗音乐数据爬取")
root.geometry("400x300")

# 创建标签和输入框
label1 = Label(root, text="请输入歌手名:")
label1.pack()
entry1 = Entry(root)
entry1.pack()

# 创建按钮
def crawl():
    # 获取输入框中的歌手名
    singer = entry1.get()
    if not singer:
        messagebox.showerror("错误", "请输入歌手名")
        return

    # 爬取酷狗音乐数据
    url = "https://www.kugou.com/yy/singer/index/{}-all-1.html".format(singer)
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "lxml")
    songs = soup.select(".song_name a")
    song_titles = [song.get("title") for song in songs]

    # 保存歌曲信息到SQLite数据库
    conn = sqlite3.connect("songs.db")
    c = conn.cursor()
    c.execute("CREATE TABLE IF NOT EXISTS songs (title TEXT)")
    for title in song_titles:
        c.execute("INSERT INTO songs (title) VALUES (?)", (title,))
    conn.commit()
    conn.close()

    # 生成词云图
    c = conn.cursor()
    c.execute("SELECT * FROM songs")
    titles = [row[0] for row in c.fetchall()]
    text = " ".join(jieba.cut(" ".join(titles)))
    wordcloud = WordCloud(background_color="white").generate(text)
    wordcloud.to_file("wordcloud.png")

    # 保存歌曲信息到Excel表中
    wb = openpyxl.Workbook()
    sheet = wb.active
    for i, title in enumerate(titles):
        sheet.cell(row=i+1, column=1, value=title)
    wb.save("songs.xlsx")

    # 显示处理后的信息
    messagebox.showinfo("信息", "已保存歌曲信息到SQLite数据库、Excel表和词云图")

button1 = Button(root, text="爬取数据", command=crawl)
button1.pack()

root.mainloop()

该代码使用了Tkinter库创建了一个简单的窗体应用系统,用户可以在输入框中输入歌手名,点击按钮后,程序会爬取该歌手的所有歌曲信息,并将信息保存到SQLite数据库、Excel表和词云图中,并弹出一个消息框显示处理后的信息

编写python代码实现以下功能爬取酷狗音乐数据主要任务:设计一个窗体应用系统具有以下功能:1加载需要用到的各种第三方库如requests;BeautifulSoup4;lxml;sqlite3;jieba;;WordCloud;openpyxl等。将信息保存到Excel表中显示处理后的信息

原文地址: https://www.cveoy.top/t/topic/hnCG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录