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()
相关推荐
进击的小头几秒前
第19篇:卡尔曼滤波器与MPC模型预测控制器的结合实战
python·算法
吴梓穆1 分钟前
UE5 c++打印日志
开发语言·c++·ue5
不会写DN1 分钟前
php 如何使用mysqli连接mysql
开发语言·mysql·php
赫瑞1 分钟前
Java中的进阶最长上升子序列——LIS
java·开发语言
吴梓穆2 分钟前
UE5 C++ 绘制图形调试宏
开发语言·c++·ue5
skywalk81633 分钟前
windows10安装python3.14
开发语言·python
2501_908329853 分钟前
C++中的装饰器模式
开发语言·c++·算法
wangchilong4 分钟前
Flask:后端框架使用
后端·python·flask
x***r1514 分钟前
Dev C++ 6.5安装与配置教程 Windows版:解压+管理员运行+自定义路径+中文设置指南
开发语言·c++
路小雨~7 分钟前
RabbitMQ基础知识
python·rabbitmq