使用 Python 刷新本地网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16399355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh a local web page using Python
提问by foosion
I'm using Python to gather some information, construct a very simple html page, save it locally and display the page in my browser using webbrowser.open('file:///c:/testfile.html'). I check for new information every minute. If the information changes, I rewrite the local html file and would like to reload the displayed page.
我正在使用 Python 收集一些信息,构建一个非常简单的 html 页面,将其保存在本地并使用 webbrowser.open('file:///c:/testfile.html') 在我的浏览器中显示该页面。我每分钟检查一次新信息。如果信息发生变化,我会重写本地 html 文件,并希望重新加载显示的页面。
The problem is that webbrowser.open opens a new tab in my browser every time I run it. How do I refresh the page rather than reopen it? I tried new=0, new=1 and new=2, but all do the same thing. Using controller() doesn't work any better.
问题是 webbrowser.open 每次运行时都会在我的浏览器中打开一个新选项卡。如何刷新页面而不是重新打开它?我尝试了 new=0、new=1 和 new=2,但都做同样的事情。使用 controller() 并没有更好的效果。
I suppose I could add something like < META HTTP-EQUIV="refresh" CONTENT="60" > to the < head > section of the html page to trigger a refresh every minute whether or not the content changed, but would prefer finding a better way.
我想我可以在 html 页面的 < head > 部分添加类似 < META HTTP-EQUIV="refresh" CONTENT="60" > 的内容,以每分钟触发一次刷新,无论内容是否更改,但更喜欢找到一个更好的方法。
Exact time interval is not important.
确切的时间间隔并不重要。
Python 2.7.2, chrome 26.0.1410.64 m, Windows 7 64.
Python 2.7.2,镀铬 26.0.1410.64 m,Windows 7 64。
回答by Dan K
It looks like several people have asked this in the past but here is a link that sums it up.
过去似乎有几个人问过这个问题,但这里有一个总结的链接。
But webbrowser.open( url, new=0 ) should open the page in the current window and not initialize a new one.
但是 webbrowser.open( url, new=0 ) 应该在当前窗口中打开页面而不是初始化一个新的。
回答by Prasanth Louis
If you're going to need a refresh on the same tab, you'll need selenium webdriver. After installing selenium using pip, you can use the following code
如果您需要在同一个选项卡上刷新,则需要 selenium webdriver。使用pip安装selenium后,可以使用如下代码
from selenium import webdriver
import time
import urllib
import urllib2
x=raw_input("Enter the URL")
refreshrate=raw_input("Enter the number of seconds")
refreshrate=int(refreshrate)
driver = webdriver.Firefox()
driver.get("http://"+x)
while True:
time.sleep(refreshrate)
driver.refresh()
This will open the url and refresh the tab every refreshrateseconds
这将打开URL并刷新选项卡每个refreshrate秒
回答by uzdisral
Keep it very short, as simple as:
保持简短,就像这样简单:
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get('URL')
while True:
time.sleep(20)
driver.refresh()
driver.quit()
回答by Reece
The LivePage extension for Chrome. You can write to a file, then LivePage will monitor it for you. You can also optionally refresh on imported content like CSS. Chrome will require that you grant permissions on local file:// urls.
Chrome 的 LivePage 扩展。您可以写入文件,LivePage 会为您监控。您还可以选择刷新导入的内容,如 CSS。Chrome 将要求您授予本地 file:// 网址的权限。
(I'm unaffiliated with the project.)
(我与该项目无关。)
回答by PSP2019
I use pyautogui module to refresh the browser page. It's one liner:
我使用 pyautogui 模块来刷新浏览器页面。这是一个班轮:
import pyautogui
pyautogui.hotkey('f5') #Simulates F5 key press = page refresh

