基于python的网页自动刷新工具

1.下载webdriver

https://msedgewebdriverstorage.z22.web.core.windows.net/?prefix=122.0.2365.59/下载Edge的浏览器驱动

2.安装selenium

python 复制代码
pip install selenium==4.11.1 

3.写代码

python 复制代码
# -*- 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)

4.运行界面

相关推荐
用户83562907805114 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng816 小时前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi16 小时前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee17 小时前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay17 小时前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤17 小时前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean18 小时前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽19 小时前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi
用户606487671889620 小时前
国内开发者如何接入 Claude API?中转站方案实战指南(Python/Node.js 完整示例)
人工智能·python·api
只与明月听20 小时前
RAG深入学习之Chunk
前端·人工智能·python