Last active
October 9, 2018 07:47
-
-
Save Benjamin1021523/2d03a55f4992ce8d5e8b111dbd81f2c0 to your computer and use it in GitHub Desktop.
每隔30分鐘觸發網頁的程式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from apscheduler.schedulers.blocking import BlockingScheduler | |
from datetime import datetime | |
def job(): | |
print("於{}開始抓取資料".format(datetime.now())) | |
requests.get('http://localhost/weather.php') | |
print("於{}結束抓取資料".format(datetime.now())) | |
job()#初次工作 | |
#定義排程工作 | |
sched = BlockingScheduler() | |
sched.add_job(job, 'interval', seconds=1800) | |
sched.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import schedule | |
import time | |
from datetime import datetime | |
import requests | |
def job(): | |
print("於{}開始抓取資料".format(datetime.now())) | |
requests.get('http://localhost/weather.php') | |
print("於{}結束抓取資料\n".format(datetime.now())) | |
schedule.every(30).minutes.do(job) | |
while True: | |
schedule.run_pending() | |
time.sleep(29) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import schedule | |
import time | |
from datetime import datetime | |
import requests | |
def job(): | |
print("於{}開始抓取資料".format(datetime.now())) | |
#requests.get('http://localhost/weather.php') | |
print("於{}結束抓取資料\n".format(datetime.now())) | |
schedule.every(30).minutes.do(job) | |
#schedule.every().day.at("07:12").do(job) | |
#schedule.every().day.at("07:42").do(job) | |
#schedule.every().day.at("08:12").do(job) | |
job() | |
while True: | |
schedule.run_pending() | |
time.sleep(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import datetime | |
import requests | |
def job(): | |
while True: | |
try: | |
a = datetime.datetime.now() | |
print("開始更新資料:{}".format(a)) | |
headers = {'Connection': 'close'} | |
requests.get('https://127.0.0.1/update.php', headers = headers) | |
time.sleep(0.01) | |
b = datetime.datetime.now() | |
print("結束更新資料:{}".format(b)) | |
c = (b - a).seconds | |
print('本次更新花費時間(秒):', c) | |
print('下次更新時間:', b + datetime.timedelta(seconds = 1800 - c)) | |
time.sleep(1800 - c) | |
except: | |
print(" 本次更新出現不明錯誤,現在時間{}".format(datetime.now())) | |
print("於五分鐘後,{},再次嘗試".format(datetime.datetime.now() + datetime.timedelta(seconds = 300))) | |
time.sleep(300) | |
job() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment