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()
相关推荐
辞旧 lekkk18 分钟前
【c++】c++11(上)
开发语言·c++·学习·萌新
程序员阿鹏20 分钟前
SpringBoot自动装配原理
java·开发语言·spring boot·后端·spring·tomcat·maven
彭世瑜23 分钟前
C/C++:libfort用于在终端输出表格
c语言·开发语言·c++
RAY_010424 分钟前
Python—数据可视化pyecharts
开发语言·python
徐同保24 分钟前
n8n+GPT-4o一次解析多张图片
开发语言·前端·javascript
春日见28 分钟前
如何跑通,吃透一个开源项目?
linux·运维·开发语言·数码相机·matlab
技术净胜33 分钟前
MATLAB数据清洗流程包含:缺失值处理/异常值检测/重复值删除
开发语言·matlab
SmoothSailingT34 分钟前
C#——textBox控件(1)
开发语言·c#
Lvan的前端笔记43 分钟前
python:用 dotenv 管理环境变量&生产环境怎么管理环境变量
网络·数据库·python
Java Fans43 分钟前
用PyQt打造带动画、碰撞检测和键盘控制的小游戏
python·计算机外设·pyqt