Python刷新HTML文档

发布于 2024-12-26 00:52:30 字数 173 浏览 5 评论 0原文

我有一个 HTML 文档,可以通过将其指定为我想要打开的 URL,使用 webbrowser 模块打开该文档,效果很好。我希望该文档能够使用将要运行的 Python 脚本来刷新自身(以重新打开自身以外的方式)。我尝试在 webbrowser 模块中设置 new=0 ,但它只是在浏览器中打开一个新选项卡。有什么办法可以做到这一点吗?

I have an HTML document that I am able to open with the webbrowser module by specifying it as the URL that I want to open, this works fine. I want this document to be able to refresh itself (in a way other than reopening itself) with a Python script that will be running. I've tried setting new=0 in the webbrowser module, but it just opens a new tab in the browser anyway. Is there any way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新一帅帅 2025-01-02 00:52:30

解决问题的两种方法:
JavaScript - 将其添加到页面底部并更新文件名路径,它将在同一选项卡中每 10 秒进行一次完全重新加载。

<script>
setInterval(function(){
window.open('file:///C:/YOURPATH/file2.html', "_self")
}, 10000);
</script>

Python 选项 - 使用 Selenium。为此,您需要下载与您所需的浏览器兼容的 driver.exe,并在代码中链接到它。使用 time.sleep(10) 在 do while true 循环中将打开的页面分配给驱动程序。

如果你坚持使用 python,我可以写出更详细的代码,但 JavaScript 解决方案应该可以工作。

Two ways to solve the issue:
JavaScript - Add this to the bottom of your page with the filename path updated, it will do a full reload every 10 seconds in the same tab.

<script>
setInterval(function(){
window.open('file:///C:/YOURPATH/file2.html', "_self")
}, 10000);
</script>

Python option - use Selenium. For that you will need to download a driver.exe compatible with the browser you need, link to it in your code. Assign the open page to the driver in a do while true loop with a time.sleep(10).

If you insist on using python, I can write out the code in more detail, but the JavaScript solution should work.

毁梦 2025-01-02 00:52:30

Ajax (Javascript) 用于刷新 html 文档而无需重新加载。

Ajax (Javascript) is used to refresh-html-document without reload .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文