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()
相关推荐
吕源林21 小时前
Golang怎么Redis发布订阅_Golang如何用Publish和Subscribe收发消息【实战】
jvm·数据库·python
用户83562907805121 小时前
使用 Python 合并与拆分 Excel 单元格的实用方法
后端·python
2301_7641505621 小时前
Golang colly爬虫框架如何用_Golang colly教程【进阶】
jvm·数据库·python
2301_803538951 天前
SQL统计各分组中排名前三的记录_使用窗口函数RANK
jvm·数据库·python
2301_782659181 天前
如何让按钮悬停时阴影位置保持固定(仅按钮位移)
jvm·数据库·python
weixin_580614001 天前
如何用 performance.navigation 判断页面刷新并清理缓存
jvm·数据库·python
量子炒饭大师1 天前
【C++ 进阶】Cyber霓虹掩体下的代码拟态——【面向对象编程 之 多态】一文带你搞懂C++面向对象编程中的三要素之一————多态!
开发语言·c++·多态
21439651 天前
Golang strings.Builder如何用_Golang Builder拼接教程【对比】
jvm·数据库·python
2301_777599371 天前
mysql如何配置主机缓存_mysql host_cache_size设置
jvm·数据库·python
qq_206901391 天前
为什么宝塔面板网站无法正常连接外部远程数据库_检查服务器安全组放行端口并开启IP授权
jvm·数据库·python