【发布时间】:2018-07-26 09:34:12
【问题描述】:
我有下面的脚本,我希望它每 30 分钟运行一次,有人可以为我指出正确的方向。
我已经搜索过这样的现有问题,但似乎没有找到任何适用于我的脚本的想法,但不知道这是否是我的愚蠢。
我的脚本在我的屏幕点击时转到不同的位置,然后进行屏幕截图,然后将图像发送到我的 gmail 帐户。
import pyautogui
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os
time.sleep(5)
pyautogui.PAUSE = 1
pyautogui.moveTo(922,134)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.moveTo(178,277)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.moveTo(178,297)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.moveTo(178,315)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.screenshot('web.png')
pyautogui.PAUSE = 5
gmail_user = "user@gmail.com"
gmail_pwd = "password"
to = "user@gmail.com"
subject = "Report"
text = "Picture report"
attach = 'web.png'
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
【问题讨论】:
-
为什么不创建一个每 30 分钟执行一次脚本的任务?
-
为什么不使用
time.sleep(30*60)将代码封装在无限循环中? -
stackoverflow.com/questions/132971/… Linux 有 crontab。我从来没有在 Windows 上使用过这样的东西,但该链接似乎有几种可能的解决方案。
-
嗨,我是 python 新手,所以我还在学习,所以不太确定如何将其编写为任务或将其置于无限循环中,请给我一个示例
标签: python python-3.x time automation