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()
相关推荐
fengxin_rou8 分钟前
深入理解Java类加载机制:从原理到实战详解
java·开发语言
薇茗10 分钟前
【C++】类与对象 核心篇
开发语言·c++
小江的记录本12 分钟前
【Spring全家桶】Spring Cloud 2023.0.x:配置中心:Nacos Config、Apollo(附《思维导图》+《面试高频考点清单》)
java·spring boot·后端·python·spring·spring cloud·面试
AI浩12 分钟前
【数据处理】基于 SAM3 的 LabelMe 标注统一校正方法
android·开发语言·kotlin
原来是猿16 分钟前
理解 C++ 哈希表的原理与工程实践
开发语言·c++·散列表
雪的季节19 分钟前
Qt 自定义表头
开发语言·qt
C137的本贾尼27 分钟前
JDBC 编程:用 Java 连接 MySQL
java·开发语言·mysql
AI视觉网奇31 分钟前
three-bvh-csg glb分割
开发语言·前端·javascript
牢姐与蒯32 分钟前
c++数据结构之c++11(二)
开发语言·c++
z2005093035 分钟前
【linux学习】深入理解 Linux 进程间通信:管道的艺术与实现
linux·开发语言