如何使以下代码每10秒执行一次

2024-04-27 03:06:42 发布

您现在位置:Python中文网/ 问答频道 /正文

import urllib2

page = urllib2.urlopen('http://127.0.0.1:5000/')

page_content = page.read()

with open('index.html', 'w') as fid:
    fid.write(page_content)

我尝试了很多方法让代码在这么多秒后再次运行,但每次尝试都会出错(windows用户)

import urllib2 
import time 
def executeSomething(): 
    page = urllib2.urlopen('127.0.0.1:5000/') 
    page_content = page.read() with open('index.html', 'w') as fid:
        fid.write(page_content) 
        time.sleep(60) 

while True: 
    executeSomething()

Tags: importreadindextimehtmlaswithpage
1条回答
网友
1楼 · 发布于 2024-04-27 03:06:42

使用^{}

import urllib2
import time

while True:
    page = urllib2.urlopen('http://127.0.0.1:5000/')
    page_content = page.read()
    with open('index.html', 'w') as fid:
        fid.write(page_content)
    time.sleep(10)

相关问题 更多 >