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()
相关推荐
狗凯之家源码网16 小时前
短剧系统搭建实战:源码功能与商业变现效果全景展示
开发语言·php
幻想时空16 小时前
地图数据采集
python
梦想不只是梦与想16 小时前
Python 中的类型判断方法
python·type·isinstance
alphaTao16 小时前
LeetCode 每日一题 2026/7/27-2026/8/2
python·算法·leetcode
易筋紫容17 小时前
创建型模式:对象的诞生艺术
开发语言·前端·javascript
888CC++17 小时前
C++ 快速学习指南:从入门到进阶的实战路线
开发语言·c++
Kevin Wang72717 小时前
Nvidia-AGX-spark部署手册——课堂质量诊断(jetpack:r36)
python·docker·容器
小小晓.17 小时前
C++记:函数
开发语言·c++·算法
牡丹雅忻117 小时前
AES 加密模式演进:从 ECB、CBC 到 GCM 的 C# 深度实践
java·开发语言·c#
连续讨伐17 小时前
php小结
开发语言·php