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 time
class WebRefresherApp:
def __init__(self, root):
self.root = root
self.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 = False
self.refresh_task=None
self.driver = webdriver.Edge()
self.timer = None
def 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.")
return
if not url:
messagebox.showerror("Invalid Input", "Target URL cannot be empty.")
return
self.url = url
self.driver.get(self.url) # 替换为你要刷新的网址
if self.refresh_task is not None:
messagebox.showerror("Invalid Input", "先停止原有任务!")
return
self.is_refreshing = True
if 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.i
self.driver.refresh()
time.sleep(self.refresh_interval)
except Exception as err:
print(err)
def stop_refreshing(self):
self.is_refreshing = False
if self.refresh_task is not None:
self.refresh_task.join()
self.refresh_task = None
def 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)