PytzUsageWarning: The normalize method is no longer necessary
在Python中使用apscheduler进行定时任务,启动定时脚本时抛出错误PytzUsageWarning: The normalize method is no longer necessary
。
原代码
from apscheduler.schedulers.blocking import BlockingScheduler
def crawler():
"""爬虫任务"""
pass
def main():
"""主函数"""
# 创建定时对象
scheduler = BlockingScheduler()
# 定义任务
scheduler.add_job(crawler, 'cron', day_of_week='tue', hour=8, minute=00)
# 启动任务
print("+++ +++ +++ 启动 +++ +++ +++")
scheduler.start()
if __name__ == '__main__':
main()
**
执行报错
C:\SmartTV>python SmartTv.py
C:\Users\yang\AppData\Local\Programs\Python\Python310\lib\site-packages\apscheduler\util.py:95: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
if obj.zone == 'local':
+++ +++ +++ 启动 +++ +++ +++
C:\Users\yang\AppData\Local\Programs\Python\Python310\lib\site-packages\apscheduler\triggers\cron\__init__.py:146: PytzUsageWarning: The normalize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
return self.timezone.normalize(dateval + difference), fieldnum
C:\Users\yang\AppData\Local\Programs\Python\Python310\lib\site-packages\apscheduler\triggers\cron\__init__.py:159: PytzUsageWarning: The localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
return self.timezone.localize(datetime(**values))
修改之后的代码
from apscheduler.schedulers.blocking import BlockingScheduler
def crawler():
"""爬虫任务"""
pass
def main():
"""主函数"""
# 创建定时对象
scheduler = BlockingScheduler(timezone="Asia/Shanghai")
# 定义任务
scheduler.add_job(crawler, 'cron', day_of_week='tue', hour=8, minute=00)
# 启动任务
print("+++ +++ +++ 启动 +++ +++ +++")
scheduler.start()
if __name__ == '__main__':
main()