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()
相关推荐
Mr_Xuhhh几秒前
深入Java多线程进阶:从锁策略到并发工具全解析
前端·数据库·python
ZK_H24 分钟前
半导体工艺流程
java·c语言·开发语言·计算机网络·金融
apcipot_rain29 分钟前
Python实战——蒙特卡洛模拟分析杀牌游戏技能收益
python·游戏·数学建模
计算机安禾30 分钟前
【数据结构与算法】第39篇:图论(三):最小生成树——Prim算法与Kruskal算法
开发语言·数据结构·c++·算法·排序算法·图论·visual studio code
liliangcsdn31 分钟前
sentence-transformer如何离线加载和使用模型
开发语言·前端·php
Crazy________32 分钟前
4.10dockerfile构建镜像
java·开发语言
老绿光33 分钟前
Python 字典完全指南:从入门到实战
linux·服务器·python
fish_xk34 分钟前
c++内存管理
开发语言·c++·算法
是小蟹呀^44 分钟前
【总结】LangChain中如何维持记忆
python·langchain·memory
独特的螺狮粉44 分钟前
城市空气质量简易指数查询卡片:鸿蒙Flutter框架 实现的空气质量查询应用
开发语言·flutter·华为·架构·harmonyos