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()
相关推荐
2301_822366352 分钟前
C++中的智能指针详解
开发语言·c++·算法
kdniao17 分钟前
PHP 页面中如何实现根据快递单号查询物流轨迹?对接快递鸟在途监控 API 实操
android·开发语言·php
郑州光合科技余经理7 分钟前
同城配送调度系统实战:JAVA微服务
java·开发语言·前端·后端·微服务·中间件·php
leaves falling9 分钟前
c语言-函数讲解
c语言·开发语言
癫狂的兔子11 分钟前
【BUG】【Python】【Spider】Compound class names are not allowed.
开发语言·python·bug
css趣多多15 分钟前
动态路由,路由重置,常量路由,$ref,表单验证流程
开发语言·javascript·ecmascript
秋深枫叶红19 分钟前
嵌入式C语言阶段复习——循环语句和分支语句
c语言·开发语言
还在忙碌的吴小二21 分钟前
Go-View 数据可视化大屏使用手册
开发语言·后端·信息可视化·golang
u01092727128 分钟前
C++中的模板方法模式
开发语言·c++·算法