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_78183352几秒前
Python 正则表达式入门教程
开发语言·python·正则表达式
gihigo19982 分钟前
基于蒙特卡洛的异常值剔除(RANSAC + MC置信区间)—MATLAB实现
开发语言·算法·matlab
Ting.~17 分钟前
在java中接入百度地图
java·开发语言·dubbo
小短腿的代码世界18 分钟前
Qt对象树析构链与智能指针协同:零泄漏内存管理架构
开发语言·qt·架构
copyer_xyf25 分钟前
Agent Tool 调用
后端·python·agent
zhaqonianzhu26 分钟前
LOL切回桌面问题,采用监控抓出元凶方式
开发语言
Aurorar0rua28 分钟前
CS50 x 2024 Notes Arrays - 04
c语言·开发语言·学习方法
Amo Xiang32 分钟前
SpiderDemo 第5题:OB混淆实战 —— 反调试绕过与 signature 签名还原
python·js逆向·爬虫逆向·反调试·spiderdemo·ob混淆
copyer_xyf33 分钟前
Agent 结构化输出
后端·python·agent
FBI HackerHarry浩43 分钟前
Ollama如何安装到D盘
python·ai