python - 在python中的特定时间运行函数
问题描述
import time
import webbrowser
print(time.ctime())
targetTime = time.ctime()
if(targetTime == "Tue May 01 11:05:17 2018"):
webbrowser.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
这是我已经尝试过的,到时候它不会打开链接。我通读了时间图书馆,但找不到任何可以帮助我的东西。我的目标是让程序在我想要的时间打开一个链接。任何帮助表示赞赏。
解决方案
Python 内置了一个简单的调度库,称为sched
支持这一点。
import sched, time
def action():
webbrowser.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
# Set up scheduler
s = sched.scheduler(time.localtime, time.sleep)
# Schedule when you want the action to occur
s.enterabs(time.strptime('Tue May 01 11:05:17 2018'), 0, action)
# Block until the action has been run
s.run()
推荐阅读
- spring-boot - DeadLetterPublishingRecoverer 中的 Kafka 序列化器与 Spring 消息转换器
- node.js - 运行 npm init 找不到模块
- python - 字符串计数方法给出不正确的结果
- curl - 如何通过命令行卷曲特定文件?
- python - 如何将 pandas 数据帧导出为二进制格式的文件并让 c++ 读取它?
- javascript - 如何通过用户 ID 在 vue-bootstrap 中打开模式窗口
- java - JavaEE MDB“EJB 容器初始化错误”
- python - 通过 python 脚本将文件上传到共享点
- maven - 为 Gephi 运行 Maven
- sql - 如何在 SQL Server 中读取 XML 重复节点