python定时任务,定时爬取水质和天气

定时爬取水质和天气

代码

c 复制代码
from apscheduler.schedulers.background import BackgroundScheduler
import requests
import datetime

urlweather = "http://localhost:8000/CrwalingViewWeather"  # 天气接口
urlwater = "http://localhost:8000/CrwalingViewWater/"  # 水质接口

def fetch_weather():
    print("正在获取天气数据...", datetime.datetime.now())
    try:
        response = requests.get(urlweather)
        if response.status_code == 200:
            print("天气数据获取成功")
        else:
            print("天气数据获取失败")
    except Exception as e:
        print(f"获取天气数据时发生错误: {e}")

def fetch_water_quality():
    print("正在获取水质数据...", datetime.datetime.now())
    try:
        response = requests.get(urlwater)
        if response.status_code == 200:
            print("水质数据获取成功")
        else:
            print("水质数据获取失败")
    except Exception as e:
        print(f"获取水质数据时发生错误: {e}")

scheduler = BackgroundScheduler()

# 每小时的第10分钟执行
scheduler.add_job(fetch_weather, 'cron', hour='*', minute=40)
# 每小时的第20分钟执行
scheduler.add_job(fetch_water_quality, 'cron', hour='*', minute=20)

scheduler.start()

try:
    # 模拟长时间运行
    while True:
        datetime.datetime.now()
except (KeyboardInterrupt, SystemExit):
    scheduler.shutdown()
相关推荐
2401_879503411 分钟前
C++中的观察者模式变体
开发语言·c++·算法
lsx20240615 分钟前
Rust 迭代器
开发语言
阿贵---21 分钟前
C++中的备忘录模式
开发语言·c++·算法
房开民26 分钟前
paddle 文本检测识别模型转为onnx
开发语言·r语言·paddle
wh_xia_jun35 分钟前
MODNet 本地抠图项目指南
python
setmoon21437 分钟前
C++中的观察者模式实战
开发语言·c++·算法
2403_8355684739 分钟前
C++代码规范化工具
开发语言·c++·算法
山上三树1 小时前
Qt Widget介绍
开发语言·qt
老歌老听老掉牙1 小时前
Python星号参数深度解析
python·参数·星号
2401_884563242 小时前
Python Lambda(匿名函数):简洁之道
jvm·数据库·python