1.下载webdriver
https://msedgewebdriverstorage.z22.web.core.windows.net/?prefix=122.0.2365.59/下载Edge的浏览器驱动
2.安装selenium
pip install selenium==4.11.1
3.写代码
# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import messagebox
import threading
from selenium import webdriver
import timeclass WebRefresherApp:def __init__(self, root):self.root = rootself.root.title("Web Refresher")self.url_label = tk.Label(root, text="Target URL:")self.url_label.pack(pady=5)self.url_entry = tk.Entry(root, width=50)self.url_entry.pack(pady=5)self.url_entry.insert(0, "https://www.bing.com/?ensearch=1&mkt=zh-CN") # 默认网址self.interval_label = tk.Label(root, text="Refresh Interval (seconds):")self.interval_label.pack(pady=5)self.interval_entry = tk.Entry(root, width=10)self.interval_entry.pack(pady=5)self.interval_entry.insert(0, "30") # 默认刷新间隔时间self.open_button = tk.Button(root, text="Open and Start Refreshing", command=self.open_and_start_refresh)self.open_button.pack(pady=10)self.stop_button = tk.Button(root, text="Stop Refreshing", command=self.stop_refreshing)self.stop_button.pack(pady=10)self.is_refreshing = Falseself.refresh_task=Noneself.driver = webdriver.Edge()self.timer = Nonedef open_and_start_refresh(self):url = self.url_entry.get()try:self.refresh_interval = int(self.interval_entry.get())except ValueError:messagebox.showerror("Invalid Input", "Refresh interval must be a number.")returnif not url:messagebox.showerror("Invalid Input", "Target URL cannot be empty.")returnself.url = urlself.driver.get(self.url) # 替换为你要刷新的网址if self.refresh_task is not None:messagebox.showerror("Invalid Input", "先停止原有任务!")returnself.is_refreshing = Trueif self.refresh_task is None:self.refresh_task = threading.Thread(target=self.refresh_page)self.refresh_task.start()def refresh_page(self):try:while self.is_refreshing:print('refresh')self.driver.iself.driver.refresh()time.sleep(self.refresh_interval)except Exception as err:print(err)def stop_refreshing(self):self.is_refreshing = Falseif self.refresh_task is not None:self.refresh_task.join()self.refresh_task = Nonedef run_tk(root):root.mainloop()# pyinstaller -F -c --uac-admin -i .\img\explorer.ico -n UpdateWeb .\update_web.py
if __name__ == "__main__":root = tk.Tk()app = WebRefresherApp(root)root.mainloop()run_tk(root)