基于python与tkinter的(招聘、教务)多网站监控程序

基于python与tkinter的(招聘、教务)多网站监控程序,第1张

利用 Python 的标准库tkinter,进行界面设计,可进行多个网站同时监控,如果网站发生了改变(比如发布新通知、公告招聘信息等),点击运行则会显示网站是否更新,详细介绍见代码,注解很清晰,主要包括三个部分:窗口创建、数据库 *** 作、网站监控 *** 作。

from tkinter import ttk
from tkinter import *
import sqlite3
import re
import requests
import datetime

class Accounting:
    # 数据库名字
    db_name = 'database.db'
    A = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj', 'kk', 'll', 'mm', 'nn', 'oo', 'pp', 'qq', 'rr']
    B = ['aa1', 'bb2', 'cc3', 'dd4', 'ee5',
         'ff6', 'gg7', 'hh8', 'ii10', 'jj11', 'kk12', 'll13', 'mm14', 'nn15', 'oo16', 'pp17', 'qq18', 'rr19']

    # 初始化 *** 作
    def __init__(self, window):
        self.win = window
        self.win.title('招聘网站监控系统 ')
        self.win.config(background="pink")

        # 创建容器
        frame = LabelFrame(self.win, text='网站监控记录')
        frame.grid(row=0, column=0, columnspan=3, pady=20)



        # 输入项目
        Label(frame, text='单位/公司名称').grid(row=1, column=0)
        self.name = Entry(frame)
        self.name.focus()
        self.name.grid(row=1, column=1)
        Label(frame, text='网址').grid(row=2, column=0)
        self.price = Entry(frame)
        self.price.grid(row=2, column=1)
        Label(frame, text='类别').grid(row=3, column=0)
        self.type = ttk.Combobox(frame, values=['国企', '私企'], state='readonly')
        self.type.grid(row=3, column=1)
        Label(frame, text='是否更新').grid(row=4, column=0)
        self.update1 = ttk.Combobox(frame, values=['未更新', '已更新'], state='readonly')
        self.update1.grid(row=4, column=1)
        self.update1.current(0)

        ttk.Button(frame, text='保存', command=self.add_accounting).grid(row=5, columnspan=2, sticky=W+E)

        # 数据 *** 作结果的提示信息
        self.message = Label(text='', fg='red')
        self.message.grid(row=3, column=0, columnspan=2, sticky=W+E)

        # treeview进行表格信息显示
        self.tree = ttk.Treeview(height=10, columns=("#0", "#1", "#2", "#3", "#4"))
        self.tree.grid(row=4, column=0, columnspan=3)
        self.tree.heading("#0", text='单位/公司名称', anchor=CENTER)
        self.tree.heading("#1", text='网址', anchor=CENTER)
        self.tree.heading("#2", text='类型', anchor=CENTER)
        self.tree.heading("#3", text='是否更新', anchor=CENTER)
        self.tree.heading("#4", text='更新时间', anchor=CENTER)

        ttk.Button(text='删除', command=self.delete_accounting).grid(row=5, column=0, sticky=W+E)
        ttk.Button(text='编辑', command=self.edit_accounting_win).grid(row=5, column=1, sticky=W + E)
        ttk.Button(text='运行', command=self.run_accounting_win).grid(row=5, column=2, sticky=W + E)
        self.get_accounting()


    # 数据库 *** 作方法
    def run_query(self, query, params=()):
        with sqlite3.connect(self.db_name) as conn:
            cursor = conn.cursor()
            result = conn.execute(query, params)
            conn.commit()
            return result

    # 数据完整性验证
    def validation(self):
        return len(self.name.get()) != 0 and len(self.price.get()) != 0

    # 数据添加处理
    def add_accounting(self):
        now = datetime.datetime.now()
        if self.validation():
            query = "insert into accounting(name, price, type, update1, time) values(?, ?, ?, ?,?)"
            params = (self.name.get(), self.price.get(), self.type.get(), self.update1.get(), now)
            self.run_query(query, params)
            self.message['text'] = "{}添加成功".format(self.name.get())
            Accounting.b = Accounting.b + 1
            print(Accounting.b)
            self.get_accounting()
        else:
            self.message['text'] = '名称和网址不能为空'

    # 显示所有项目
    def get_accounting(self):
        # 重绘表格内容
        records = self.tree.get_children()
        for element in records:
            self.tree.delete(element)

        query = "select * from accounting order by time"
        db_rows = self.run_query(query)
        # 填充表格
        for row in db_rows:
            self.tree.insert("", 0, text=row[1], values=(row[2], row[3], row[4], row[5]))

    # 删除项目
    def delete_accounting(self):
        self.message['text'] = ''
        try:
            self.tree.item(self.tree.selection())['text'][0]
        except IndexError as e:
            self.message['text'] = '请选择 *** 作项目'
            return
        self.message['text'] = ''
        name = self.tree.item(self.tree.selection())['text']
        query = 'delete from accounting where name = ?'
        self.run_query(query, (name, ))
        self.message['text'] = '{}删除成功'.format(name)
        Accounting.b = Accounting.b - 1
        print(Accounting.b)
        self.get_accounting()

    # 运行项目  重点是读取db文件的内容
    def run_accounting_win(self):
        conn = sqlite3.connect(self.db_name)
        cursor = conn.cursor()
        spl = 'select * from accounting'
        i = 0
        try:
            cursor.execute(spl)
            person_all = cursor.fetchall()
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 \
                            (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1'}
            for p in person_all:
                req = requests.get(p[2], headers=headers)  # 文件的保存名字如何更改
                Accounting.A[i] = re.compile('.html(.*?)').findall(req.text)  # 文件的保存名字如何更改
                i = i + 1
            print('已获取当前网页数据')
            while True:
                i = 0
                for p in person_all:
                    req = requests.get(p[2], headers=headers)
                    Accounting.B[i] = re.compile('.html(.*?)').findall(req.text)
                    pname = p[1]
                    old_update = p[4]
                    old_time = p[5]
                    if (Accounting.B[i] != Accounting.A[i]):  # 判断内容列表是否更新
                        Accounting.A[i] = Accounting.B[i]  # 原始内容列表改变
                        state = "已更新"
                        self.edit_state(pname, state, old_update, old_time)
                        #  打印到上位机
                    else:
                        state = "未更新"
                        self.edit_state(pname, state, old_update, old_time)
                    i = i + 1
                window.mainloop()
        except Exception as e:
            print(e)
            print('查询失败')
        finally:
            cursor.close()

    # 数据修改处理
    def edit_state(self, pname, state,old_update, old_time):
        now = datetime.datetime.now()
        query = "update accounting set update1=?, time=? where update1=? and time=?"
        params = (state, now, old_update, old_time)
        self.run_query(query, params)
        self.message['text'] = "全网站更新成功".format(pname)
        self.get_accounting()

    # 修改的窗体实现
    def edit_accounting_win(self):
        self.message['text'] = ''
        try:
            self.tree.item(self.tree.selection())['text'][0]
        except IndexError as e:
            self.message['text'] = '请选择 *** 作项目'
            return
        old_name = self.tree.item(self.tree.selection())['text']
        old_price = self.tree.item(self.tree.selection())['values'][0]
        old_type = self.tree.item(self.tree.selection())['values'][1]
        old_update1 = self.tree.item(self.tree.selection())['values'][2]
        old_time = self.tree.item(self.tree.selection())['values'][3]

        self.edit_win = Toplevel()
        self.edit_win.title = '编辑项目'

        # 原来的项目
        Label(self.edit_win, text='原单位/公司').grid(row=0, column=1)
        Entry(self.edit_win, textvariable=StringVar(self.edit_win, value=old_name), state='readonly').grid(row=0, column=2)

        Label(self.edit_win, text='新单位/公司').grid(row=1, column=1)
        new_name = Entry(self.edit_win)
        new_name.grid(row=1, column=2)

        Label(self.edit_win, text='原网址').grid(row=2, column=1)
        Entry(self.edit_win, textvariable=StringVar(self.edit_win, value=old_price), state='readonly').grid(row=2, column=2)

        Label(self.edit_win, text='新网址').grid(row=3, column=1)
        new_price = Entry(self.edit_win)
        new_price.grid(row=3, column=2)

        Label(self.edit_win, text='类别').grid(row=4, column=1)
        new_type = ttk.Combobox(self.edit_win, values=['国企', '私企'], state='readonly')
        new_type.grid(row=4, column=2)
        new_type.set(old_type)

        Label(self.edit_win, text='是否更新').grid(row=5, column=1)
        new_update1 = ttk.Combobox(self.edit_win, values=['未更新', '已更新'], state='readonly')
        new_update1.grid(row=5, column=2)
        new_update1.set(old_update1)

        new_time = datetime.datetime.now()

        Button(self.edit_win, text='更新', command=lambda: self.edit_record(new_name.get(), old_name, new_price.get(), old_price, new_type.get(), old_type, new_update1.get(), old_update1, new_time, old_time)).grid(row=6, column=2, sticky=W+E)

        self.edit_win.mainloop()

    # 数据修改处理
    def edit_record(self, new_name, old_name, new_price, old_price, new_type, old_type, new_update1, old_update1, new_time, old_time):
        query = "update accounting set name=?, price=?, type=?, update1=?, time=? where name=? and price=? and type=? and update1=? and time=?"
        params = (new_name, new_price, new_type, new_update1, new_time, old_name, old_price, old_type, old_update1, old_time)
        self.run_query(query, params)
        self.edit_win.destroy()
        self.message['text'] = "{}更新成功".format(old_name)
        self.get_accounting()

if __name__ == '__main__':
    window = Tk()
    application = Accounting(window)
    window.mainloop()

最后结果界面所示:

 

才疏学浅,有不足之处,请见谅~~

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/755936.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-30
下一篇 2022-04-30

发表评论

登录后才能评论

评论列表(0条)

保存