jinzhudada 6 months ago
parent 55945f2efe
commit 70c9d8561e

@ -0,0 +1,46 @@
from tkinter import *
import mysql
user_login = {'aaa':'123456','bbb':'123321','ccc':'345543'}
count = 0
def login():
global count
username = entry_username.get()
if username not in user_login:
Label_message.config(text='账号错误')
else:
password = entry_password.get()
if (password==user_login[username]):
Label_message.config(text='登录成功')
mysql.query_database()
else:
Label_message.config(text='密码错误!还可以尝试{}'.format(2-count))
count += 1
if count == 3:
Label_message.config(text='登录失败')
btn_login.config(stae = "disabled")
window = Tk()
window.title("用户登录")
window.geometry('300x200')
Label_username = Label(window, text='用户名')
Label_username.pack()
entry_username = Entry(window)
entry_username.pack()
Label_password = Label(window, text='密码')
Label_password.pack()
entry_password = Entry(window, show='*')
entry_password.pack()
btn_login = Button(window, text='登录', command=login)
btn_login.pack()
Label_message = Label(window, text='')
Label_message.pack()
window.mainloop()

@ -0,0 +1,52 @@
import requests
import re
import tkinter as tk
from tkinter import messagebox
from tkinter import Toplevel
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 fetch_data():
urls = [f"https://www.gushiwen.cn/default_{i}.aspx" for i in range(1, 3)]
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})
# 保存到TXT文件
with open("gushi.txt", "w", encoding="utf-8") as file:
for item in gushici:
file.write(f"标题: {item['title']}, 作者: {item['author']}, 朝代: {item['dynasty']}, 内容: {item['poetic']}\n")
# 在新窗口显示数据
show_data_window(gushici)
def show_data_window(data):
window = Toplevel(root)
window.title("古诗词信息")
text_widget = tk.Text(window)
text_widget.pack(expand=True, fill='both')
for item in data[:20]: # 仅显示前10条数据作为示例
text_widget.insert(tk.END, f"标题: {item['title']}\n作者: {item['author']}\n朝代: {item['dynasty']}\n内容: {item['poetic']}\n\n")
window.mainloop()
root = tk.Tk()
root.title("古诗词爬虫")
fetch_button = tk.Button(root, text="开始爬取", command=fetch_data)
fetch_button.pack(pady=20)
root.mainloop()

@ -0,0 +1,35 @@
import pymysql
def query_database():
config = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': '123321',
'db': 'gushici',
'charset': 'utf8mb4',
}
try:
# 创建连接
connection = pymysql.connect(**config)
print("数据库连接成功")
# 创建游标用于执行SQL命令
with connection.cursor() as cursor:
# 执行一个查询示例
cursor.execute("SELECT * FROM shici LIMIT 5")
# 获取查询结果
result = cursor.fetchall()
for row in result:
print(row)
# 自动提交事务,对于只读操作这不是必需的,但确保任何更改会生效
connection.commit()
except pymysql.MySQLError as e:
print(f"数据库查询过程中发生错误: {e}")
finally:
# 连接关闭已经在with语句中自动完成这里不需要额外判断
pass
Loading…
Cancel
Save