Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh a local web page using Python

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.

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.

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.

like image 508
foosion Avatar asked May 06 '13 13:05

foosion


People also ask

How do you refresh a webpage in Python?

To refresh a web page using the selenium module in Python, we use the refresh() function. This is shown in the code below. The refresh() will refresh the web page, acting just like the refresh button of a web browser or clicking the 'F5' button on your keyboard.

How do I refresh a webpage using selenium Python?

We can refresh a webpage using Selenium webdriver in Python. This can be done with the help of the refresh method. First of all, we have to launch the application with the get method. Once a web page is loaded completely, we can then refresh the page with the help of the refresh method.

How do I refresh a web page every 5 seconds?

setTimeout(function(){ window. location. reload(); }, 5000); This example sets 5 seconds to reload the page, you can set the time as per your needs.


3 Answers

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

This will open the URL and refresh the tab every refreshrate seconds

like image 54
Prasanth Louis Avatar answered Oct 13 '22 07:10

Prasanth Louis


I use pyautogui module to refresh the browser page. It's one liner:

import pyautogui

pyautogui.hotkey('f5') #Simulates F5 key press = page refresh
like image 28
PSP2019 Avatar answered Oct 13 '22 07:10

PSP2019


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()
like image 23
uzdisral Avatar answered Oct 13 '22 08:10

uzdisral