|
|
|
@ -0,0 +1,123 @@
|
|
|
|
|
import tkinter as tk
|
|
|
|
|
from tkinter import messagebox
|
|
|
|
|
import requests
|
|
|
|
|
import re
|
|
|
|
|
from tkinter import Toplevel
|
|
|
|
|
import pymysql
|
|
|
|
|
from tkinter import Tk
|
|
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
|
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.3.1311 SLBChan/11"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def start_mysql_process():
|
|
|
|
|
config = {
|
|
|
|
|
'host': 'localhost',
|
|
|
|
|
'port': 3306,
|
|
|
|
|
'user': 'root',
|
|
|
|
|
'password': '123321',
|
|
|
|
|
'db': 'gushici',
|
|
|
|
|
'charset': 'utf8mb4',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 创建连接
|
|
|
|
|
connection = pymysql.connect(**config)
|
|
|
|
|
print("数据库连接成功")
|
|
|
|
|
|
|
|
|
|
# 显示连接成功的消息框
|
|
|
|
|
show_success_popup()
|
|
|
|
|
|
|
|
|
|
# 创建游标,用于执行SQL命令
|
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
|
# 执行一个查询示例
|
|
|
|
|
cursor.execute("SELECT * FROM poems LIMIT 5")
|
|
|
|
|
|
|
|
|
|
# 获取查询结果
|
|
|
|
|
result = cursor.fetchall()
|
|
|
|
|
for row in result:
|
|
|
|
|
print(row)
|
|
|
|
|
|
|
|
|
|
# 对于只读操作,commit不是必需的,但保持原样以确保其他类型操作的完整性
|
|
|
|
|
connection.commit()
|
|
|
|
|
|
|
|
|
|
except pymysql.MySQLError as e:
|
|
|
|
|
print(f"数据库查询过程中发生错误: {e}")
|
|
|
|
|
finally:
|
|
|
|
|
# 连接关闭已经在with语句中自动完成,这里不需要额外处理
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def show_success_popup():
|
|
|
|
|
"""显示数据库连接成功的消息框"""
|
|
|
|
|
root = Tk()
|
|
|
|
|
root.withdraw() # 隐藏主窗口,只显示消息框
|
|
|
|
|
messagebox.showinfo("连接状态", "数据库连接成功!")
|
|
|
|
|
root.destroy() # 销毁窗口对象,结束进程
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_data():
|
|
|
|
|
urls = [f"https://www.gushiwen.cn/default_{i}.aspx" for i in range(1, 4)]
|
|
|
|
|
gushici = []
|
|
|
|
|
for url in urls:
|
|
|
|
|
response = requests.get(url, headers=headers)
|
|
|
|
|
content = response.text
|
|
|
|
|
titles = re.findall('<b>(.*?)</b>', content, re.DOTALL)
|
|
|
|
|
authors = re.findall('<p class="source">.*?<a.*?<a.*?>(.*?)</a>', content, re.DOTALL)
|
|
|
|
|
dynastys = re.findall('<p class="source">.*?<a.*?<a.*?>(.*?)</a>', content, re.DOTALL)
|
|
|
|
|
poetics = re.findall('<div class="contson.*?">(.*?)</div>', content, re.DOTALL)
|
|
|
|
|
new_poetics = [''.join(re.split('<.*?>|<.*? />', p)).strip() for p in poetics]
|
|
|
|
|
|
|
|
|
|
for title, author, dynasty, poetic in zip(titles, authors, dynastys, new_poetics):
|
|
|
|
|
gushici.append({"title": title, "author": author, "dynasty": dynasty, "poetic": poetic})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open("sci.txt", "w", encoding="utf-8") as file:
|
|
|
|
|
for item in gushici:
|
|
|
|
|
file.write(f"标题: {item['title']}, 作者: {item['author']}, 朝代: {item['dynasty']}, 内容: {item['poetic']}\n")
|
|
|
|
|
|
|
|
|
|
windon_ck(gushici)
|
|
|
|
|
|
|
|
|
|
def windon_ck(data):
|
|
|
|
|
window = Toplevel(app)
|
|
|
|
|
window.title("古诗词信息")
|
|
|
|
|
text_widget = tk.Text(window)
|
|
|
|
|
text_widget.pack(expand=True, fill='both')
|
|
|
|
|
|
|
|
|
|
for item in data[:10]: # 仅显示前10条数据作为示例
|
|
|
|
|
text_widget.insert(tk.END, f"标题: {item['title']}\n作者: {item['author']}\n朝代: {item['dynasty']}\n内容: {item['poetic']}\n\n")
|
|
|
|
|
|
|
|
|
|
window.mainloop()
|
|
|
|
|
def login():
|
|
|
|
|
entered_username = username_entry.get()
|
|
|
|
|
entered_password = password_entry.get()
|
|
|
|
|
|
|
|
|
|
if entered_username == "aaa" and entered_password == "123456":
|
|
|
|
|
messagebox.showinfo("登录成功", "欢迎回来!")
|
|
|
|
|
fetch_data()
|
|
|
|
|
start_mysql_process() and show_success_popup()
|
|
|
|
|
else:
|
|
|
|
|
messagebox.showerror("登录失败", "账号或密码错误,请重试。")
|
|
|
|
|
|
|
|
|
|
# 创建主窗口
|
|
|
|
|
app = tk.Tk()
|
|
|
|
|
app.title("登录界面")
|
|
|
|
|
# 设置窗口大小
|
|
|
|
|
app.geometry("300x150")
|
|
|
|
|
|
|
|
|
|
# 创建标签和输入框
|
|
|
|
|
username_label = tk.Label(app, text="用户名:")
|
|
|
|
|
username_label.pack()
|
|
|
|
|
username_entry = tk.Entry(app)
|
|
|
|
|
username_entry.pack()
|
|
|
|
|
|
|
|
|
|
password_label = tk.Label(app, text="密码:")
|
|
|
|
|
password_label.pack()
|
|
|
|
|
password_entry = tk.Entry(app, show="*") # 密码显示为星号
|
|
|
|
|
password_entry.pack()
|
|
|
|
|
|
|
|
|
|
# 创建登录按钮
|
|
|
|
|
login_button = tk.Button(app, text="登录", command=login)
|
|
|
|
|
login_button.pack()
|
|
|
|
|
|
|
|
|
|
# 运行应用程序的主循环
|
|
|
|
|
app.mainloop()
|