python爬虫项目以及内容查询(二)

python爬虫项目以及内容查询(二)

上一篇介绍了python爬虫项目以及内容查询(一),现在继续我们的查询操作

效果的呈现:

image5a5fc.png

界面的编写:

界面使用python的tkinter可视化GUI来写的,主要操作是连接本地数据库后进行标题标签作者等3个单个查询,和3个组合查询

from tkinter import *
from tkinter import ttk
import tkinter as tk
import pymysql
from PIL import ImageTk, Image
from tkinter.messagebox import *
#创建窗口
root=Tk()

#窗口大小 几何 宽高这里是x
root.geometry('1036x571')#窗口大小
root.iconbitmap('favicon.ico')#界面图标
columns=("id","title","url","tag","author","desction")
root.title("Huang查询系统")#界面标题
canvas = tk.Canvas(root, width=1036, height=571, bd=0)
imgpath = 'bg.gif'#背景图片
img = Image.open(imgpath)
photo = ImageTk.PhotoImage(img)

#canvas.create_image(520, 300, image=photo)
canvas.create_image(520, 300, image=photo)
canvas.pack()
entry = tk.Entry(root, insertbackground='blue', highlightthickness=2)
entry.pack()




#标签
Label(root,text='欢迎来到huang查询系统',fg='red',font=('宋体',15)).place(relx=0.4,rely=0.1,width=230)
Label(root, text="按标题查询:").place(relx=0.1, rely=0.25, relwidth=0.1)
Label(root, text="按标签查询:").place(relx=0.1, rely=0.35, relwidth=0.1)
Label(root, text="按作者查询:").place(relx=0.1, rely=0.45, relwidth=0.1)

#输入框
selecttitle = StringVar()
selecttag = StringVar()
selectauthor = StringVar()
Entry(root, textvariable=selecttitle).place(relx=0.25, rely=0.25, relwidth=0.37, height=25)
Entry(root, textvariable=selecttag).place(relx=0.25, rely=0.35, relwidth=0.37, height=25)
Entry(root, textvariable=selectauthor).place(relx=0.25, rely=0.45, relwidth=0.37, height=25)

#设置显示所有的函数
def showAllInfo():
   x = dataTreeview.get_children()
   for item in x:
       dataTreeview.delete(item)
   con = pymysql.connect('localhost', '数据库账号', '数据库密码', '数据库名',port = 3306,charset = 'utf8')
   cur = con.cursor()
   cur.execute("select * from test")
   lst = cur.fetchall()
   for item in lst:
       dataTreeview.insert("", 1, text="line1", values=item)
   cur.close()
   con.close()

#按标题查询
def showTitle():
   if selecttitle.get() == "":
       showerror(title='提示', message='输入不能为空')
   else:
       x = dataTreeview.get_children()
       for item in x:
           dataTreeview.delete(item)
       con = pymysql.connect('localhost', '数据库账号', '数据库密码', '数据库名',port = 3306,charset = 'utf8')
       cur = con.cursor()
       content="'%"+selecttitle.get()+"%'"	#进行模糊查询
       cur.execute("select * from test where title like "+content)
       lst = cur.fetchall()
       if len(lst) == 0:  #判断如果查询不到则提示查询不到窗口
           showerror(title='提示', message='查询不到数据')
       else:#否则显示查询几条记录窗口
           showinfo(title='提示', message='查询到'+str(len(lst))+"条数据")
           for item in lst:
               dataTreeview.insert("", 1, text="line1", values=item)
       cur.close()
       con.close()

#按标签查询
def showTag():
   if selecttag.get() == "":
       showerror(title='提示', message='输入不能为空')
   else:
       x = dataTreeview.get_children()
       for item in x:
           dataTreeview.delete(item)
       con = pymysql.connect('localhost', '数据库账号', '数据库密码', '数据库名',port = 3306,charset = 'utf8')
       cur = con.cursor()
       content="'%"+selecttag.get()+"%'"
       cur.execute("select * from test where tag like "+content)
       lst = cur.fetchall()
       if len(lst) == 0:
           showerror(title='提示', message='查询不到数据')
       else:
           showinfo(title='提示', message='查询到' + str(len(lst)) + "条数据")
           for item in lst:
               dataTreeview.insert("", 1, text="line1", values=item)
       cur.close()
       con.close()

#按作者查询
def showAuthor():
   if selectauthor.get() == "":
       showerror(title='提示', message='输入不能为空')
   else:
       x = dataTreeview.get_children()
       for item in x:
           dataTreeview.delete(item)
       con = pymysql.connect('localhost', '数据库账号', '数据库密码', '数据库名',port = 3306,charset = 'utf8')
       cur = con.cursor()
       content="'%"+selectauthor.get()+"%'"
       cur.execute("select * from test where author like "+content)
       lst = cur.fetchall()
       if len(lst) == 0:
           showerror(title='提示', message='查询不到数据')
       else:
           showinfo(title='提示', message='查询到' + str(len(lst)) + "条数据")
           for item in lst:
               dataTreeview.insert("", 1, text="line1", values=item)
       cur.close()
       con.close()

#按标题+标签查询
def OrderTiTa():
   if selecttitle.get() == "" or selecttag.get() == "":
       showerror(title='提示', message='标题和标签输入不能为空')
   else:
       x = dataTreeview.get_children()
       for item in x:
           dataTreeview.delete(item)
       con = pymysql.connect('localhost', '数据库账号', '数据库密码', '数据库名',port = 3306,charset = 'utf8')
       cur = con.cursor()
       content="'%"+selecttitle.get()+"%' and tag like '%"+selecttag.get()+"%'"
       cur.execute("select * from test where title like "+content)
       lst = cur.fetchall()
       if len(lst) == 0:
           showerror(title='提示', message='查询不到数据')
       else:
           showinfo(title='提示', message='查询到' + str(len(lst)) + "条数据")
           for item in lst:
               dataTreeview.insert("", 1, text="line1", values=item)
       cur.close()
       con.close()
#按标题+作者查询
def OrderTiAu():
   if selecttitle.get() == "" or selectauthor.get() == "":
       showerror(title='提示', message='标题和作者输入不能为空')
   else:
       x = dataTreeview.get_children()
       for item in x:
           dataTreeview.delete(item)
       con = pymysql.connect('localhost', '数据库账号', '数据库密码', '数据库名',port = 3306,charset = 'utf8')
       cur = con.cursor()
       content="'%"+selecttitle.get()+"%' and author like '%"+selectauthor.get()+"%'"
       cur.execute("select * from test where title like "+content)
       lst = cur.fetchall()
       if len(lst) == 0:
           showerror(title='提示', message='查询不到数据')
       else:
           showinfo(title='提示', message='查询到' + str(len(lst)) + "条数据")
           for item in lst:
               dataTreeview.insert("", 1, text="line1", values=item)
       cur.close()
       con.close()


#按标签+作者查询
def OrderTaAu():
   if selecttag.get() == "" or selectauthor.get() == "":
       showerror(title='提示', message='标签和作者输入不能为空')
   else:
       x = dataTreeview.get_children()
       for item in x:
           dataTreeview.delete(item)
       con = pymysql.connect('localhost', '数据库账号', '数据库密码', '数据库名',port = 3306,charset = 'utf8')
       cur = con.cursor()
       content="'%"+selecttag.get()+"%' and author like '%"+selectauthor.get()+"%'"
       cur.execute("select * from test where tag like "+content)
       lst = cur.fetchall()
       if len(lst) == 0:
           showerror(title='提示', message='查询不到数据')
       else:
           showinfo(title='提示', message='查询到' + str(len(lst)) + "条数据")
           for item in lst:
               dataTreeview.insert("", 1, text="line1", values=item)
       cur.close()
       con.close()





#点击排序的函数--------这是点id字段会出现排序功能
def treeview_sort_column(tv, col, reverse):#Treeview、列名、排列方式
   l = [(tv.set(k, col), k) for k in tv.get_children('')]
   print(tv.get_children(''))
   l.sort(reverse=reverse)#排序方式
   # rearrange items in sorted positions
   for index, (val, k) in enumerate(l):#根据排序后索引移动
       tv.move(k, '', index)
       print(k)
   tv.heading(col, command=lambda: treeview_sort_column(tv, col, not reverse))#重写标题,使之成为再点倒序的标题

#显示列表的控件
dataTreeview = ttk.Treeview(root, show='headings', column=('id', 'title', 'url', 'tag','author','desction'))
dataTreeview.column('id', width=50,anchor="center")
dataTreeview.column('title', width=250, anchor="center")
dataTreeview.column('url', width=150, anchor="center")
dataTreeview.column('tag', width=150, anchor="center")
dataTreeview.column('author', width=80, anchor="center")
dataTreeview.column('desction', width=150, anchor="center")

dataTreeview.heading('id', text='编号')
dataTreeview.heading('title', text='标题')
dataTreeview.heading('url', text='地址')
dataTreeview.heading('tag', text='标签')
dataTreeview.heading('author', text='作者')
dataTreeview.heading('desction', text='描述')
#显示滚动条
#dataTreeview.VScroll1 = Scrollbar(root, orient='vertical',command=dataTreeview.yview)
#dataTreeview.VScroll1.place(relx=0.971, rely=0.028, relwidth=0.024, relheight=0.958)
dataTreeview.place(rely=0.6, relwidth=0.9,relx=0.05)



for col in columns:#给所有标题加(循环上边的“手工”)
   dataTreeview.heading(col, text=col, command=lambda _col=col: treeview_sort_column(dataTreeview, _col, False))

#八个按钮控件
Button(root, text="显示所有信息", command=showAllInfo).place(relx=0.8, rely=0.3, width=100,height=80)
Button(root, text="标题查询", command=showTitle).place(relx=0.68, rely=0.25, width=100)
Button(root, text="标签查询", command=showTag).place(relx=0.68, rely=0.35, width=100)
Button(root, text="作者查询", command=showAuthor).place(relx=0.68, rely=0.45, width=100)
Button(root, text="标题+标签查询", command=OrderTiTa).place(relx=0.18, rely=0.52, width=150)
Button(root, text="标题+作者查询", command=OrderTiAu).place(relx=0.38, rely=0.52, width=150)
Button(root, text="标签+作者查询", command=OrderTaAu).place(relx=0.58, rely=0.52, width=150)
# 显示
root.mainloop()

关于界面的美化问题:

  1. 显示列表控件他的高度是没办法改变的,因此设置的位置在下方
  2. tkinter控件的背景是无法去除的,所以可以看到label标签底部还有背景颜色没有去除
  3. tkinter的控件位置是无法可视化去调动的,只能靠你去一点点的调relx或者rely的大小
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论
请先登录 后发表评论~
©️2021 CSDN 皮肤主题: 1024 设计师:白松林 返回首页

打赏作者

Code煌

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值