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()
相关推荐
迷迭香yy3 小时前
行业板块轮动因子实战从板块资金到因子建模的本地化Python全流程
数据库·人工智能·python
W_326003 小时前
Python文件进阶:一维数据与 CSV 文件读写
开发语言·python
Tyler_TXZ3 小时前
C++C语言之——二叉树
c语言·开发语言·数据结构·c++·二叉树
不会代码的小猴4 小时前
C++新增关键字
开发语言·c++·笔记
yaoxin5211234 小时前
497. Java 反射 - 使用反射读取注解
java·开发语言·python
2019一路前行4 小时前
Python 函数式编程
开发语言·python
专注仿真4 小时前
问答大模型技术方案算法实现-RAPTOR树构建算法与BEG集成使用
python·算法
冯汉栩5 小时前
Swift Control View,Label渐变颜色(源码)
开发语言·ios·swift
从小就看凹凸曼^o^6 小时前
Python基础5 - 字典与集合:(1)字典
开发语言·python
Herbert_hwt6 小时前
第七章 Java深入理解枚举类型
java·开发语言·算法