Refresh your local webpage with Python

I use Python to collect some information, create 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 overwrite the local html file and want to reload the displayed page.

The problem is that webbrowser.open opens a new tab in my browser every time I launch it. How to refresh a page, and not reopen it? I tried new = 0, new = 1 and new = 2, but they all do the same. Using a controller () does not work better.

I suppose I could add something like <META HTTP-EQUIV = "refresh" CONTENT = "60"> to the <head> on the html page to update every time if the content has changed, but would rather find The best way.

The exact time interval is not important.

Python 2.7.2, chrome 26.0.1410.64 m, Windows 7 64.

+9
source share
5 answers

If you need an update on the same tab, you will need the selenium web editor. 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() 

This will open the url and refresh the tab every refreshrate second

+3
source

It seems like several people have asked about this in the past, but here is a link that summarizes it.

Python update HTML document

But webbrowser.open (url, new = 0) should open the page in the current window and not initialize a new one.

0
source

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() 
0
source

LivePage extension for Chrome. You can write to a file, then LivePage will follow you. You can also update imported content, such as CSS, if you wish. Chrome will require that you grant permissions for the local file: // urls.

(I am not related to the project.)

0
source

I am using the pyautogui module to refresh the browser page. This is one liner:

 import pyautogui pyautogui.hotkey('f5') #Simulates F5 key press = page refresh 
-one
source

All Articles