From 70c9d8561e956a5f478289630bbe27e8ecbfd985 Mon Sep 17 00:00:00 2001 From: jinzhudada <2897439727@qq.com> Date: Mon, 27 May 2024 11:22:09 +0800 Subject: [PATCH] j5/27 --- .../__pycache__/mysql.cpython-312.pyc | Bin 0 -> 1269 bytes pyton_kese.py/login.py | 46 ++++++++++++++++ pyton_kese.py/mian.py | 52 ++++++++++++++++++ pyton_kese.py/mysql.py | 35 ++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 pyton_kese.py/__pycache__/mysql.cpython-312.pyc create mode 100644 pyton_kese.py/login.py create mode 100644 pyton_kese.py/mian.py create mode 100644 pyton_kese.py/mysql.py diff --git a/pyton_kese.py/__pycache__/mysql.cpython-312.pyc b/pyton_kese.py/__pycache__/mysql.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..08bcafc3a5dec2525493cca3c40a075e182a9ec6 GIT binary patch literal 1269 zcmZWpZ)h837=PaTlU&kV7TcyRwCPT!l?WEQS((|eH2njEwlX_EbVN&fcU><_a_M__ zW|2hpL9n{nb+|D^U57YO%4|Q1ei+72;+K{c8qR$fx@Mi9q7?@17oUqsJL+@X^ZuTH zzvp>gxcgNUy@2w|@8i?&Z~%WeMqQX1*lt8{0~An563&9cd;n)jj)d9APQsX6ORFAk z)GWT+^Q2ls6h>hcPT>`o;{L)_01ucL`A-t{4s{2fN`nS@_?pa<0x|h|KoOQ|RRr=R z;dD75OEtR}3QV3!cn*ZoR6I{{Fo}l$XVf_gY@Qv2w?%*gw?9Lk+poWTh;!z^jDu3= zAkKZ^vsBxWKn{*T4;UoN5SRdh&t>lgb^@~G2$*{`3I8741J40%EAac1DBe8(Ief|N z&ohCY3jF(gu6AZg7V6w}6!wkTo}@0vlQX(uYJd6wY>w)>X}fxk9y@ll$9A8yj2TVV zY`0}jA5Tra5_Z{~lWcPtoual3mD)lkZWvjeDmJ4`*=~6zP7T$xf;evZ`bzne?>2w< zbnEWT^8D@c+`{I^i|@6M4~+~BoRMCXhTnW+RI0X+Mox{MIwSR3T~C*l7jJJZf39i`FHlLwd8h4N_YUtE< zs~1$+GF4lcR!y8OnZ%HuN@=F;9nFoO9vPxkr??n;)6?2H+gl^j^fYb4VQd#w4J&Ed zEY-6%Q4JS5rK;=<%8}Sxcn3zTUrA|c%`j=))M;!`HO`xQCMN5O+WnFo%jC=%J?$KN zdh)z#sNI=dtU5OW+dpfmG&iZl&G=Lt**dX-^UW9~R2ShvpuH4mE(Kaj%^f9wYpJQ@ zFVXFHT^z17w(#P^V8@NgtCMTN?pyDon)BWl#8vq1w>1^%`Tv2#uAyeCQ(@Oq?M z!w-CoSH?aXTWvqS?t2Y0TB_Cef?adNrPk2jK*aV1_I@C^AYb=ZShOkvh^-sG(3&r_ z?mPS>h%6NeJmQmQ9psSi{?i7&rj3TsT7yHtpL8Y&yt*>GBCq-*8^VcI;RJT_h`SXJ zMt70XBkmaexZI_eqs{PRKNl6)pZWyEMS+h#<1UJB2RAoG``F?O{%C|P_HqLZ6eGOj z>0=T9%rFBS8)j$#d8$`N4(.*?)', content, re.DOTALL) + authors = re.findall('

.*?(.*?)', content, re.DOTALL) + dynastys = re.findall('

.*?(.*?)', content, re.DOTALL) + poetics = re.findall('

(.*?)
', 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() \ No newline at end of file diff --git a/pyton_kese.py/mysql.py b/pyton_kese.py/mysql.py new file mode 100644 index 0000000..c08050e --- /dev/null +++ b/pyton_kese.py/mysql.py @@ -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