python 本地调用网页_使用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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值