使用Python刷新本地网页 [英] Refresh a local web page using Python
问题描述
我正在使用Python收集一些信息,构建一个非常简单的html页面,将其保存在本地,并使用webbrowser.open('file:///c:/testfile.html')在我的浏览器中显示该页面.我每分钟都会检查一次新信息.如果信息更改,我将重写本地html文件,并希望重新加载显示的页面.
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.
问题是webbrowser.open每次运行时都会在浏览器中打开一个新选项卡.如何刷新页面而不是重新打开页面?我尝试过new = 0,new = 1和new = 2,但是它们都做相同的事情.使用controller()不会更好.
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.
我想我可以添加类似<的内容. META HTTP-EQUIV =刷新" CONTENT ="60">到< html页面的head>部分可触发每分钟刷新,无论内容是否更改,但都希望找到更好的方法.
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.
确切的时间间隔并不重要.
Exact time interval is not important.
Python 2.7.2,chrome 26.0.1410.64 m,Windows 7 64.
Python 2.7.2, chrome 26.0.1410.64 m, Windows 7 64.
推荐答案
如果您需要在同一选项卡上进行刷新,则需要使用硒webdriver. 使用pip安装selenium之后,您可以使用以下代码
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
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()
这将打开网址,并每refreshrate
秒刷新一次标签页
This will open the url and refresh the tab every refreshrate
seconds
这篇关于使用Python刷新本地网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!