python自动截图发送邮件_Python 定时自动截屏,发邮件通知

前一段做试验,需要远程监控现场的电脑;

写了一个定时截屏并把图片发送至邮箱的脚本,这样微信就可以得到通知。

1. 截屏,并保存图片

from PIL import ImageGrab

import time

time_now = time.strftime('%Y%m%d-%H%M%S')

pic = ImageGrab.grab()

pic_name = time_now+'.jpg'

pic.save(pic_name)

2. 定时任务

import threading

def func():

t = time.localtime(time.time())

min, hour, wkday = t.tm_min, t.tm_hour, t.tm_wday

# --------add tasks here--------------------------

if min in [0, 15, 30, 45]:

print('tasks')

# --------tasks end----------------------------

global timer

timer = threading.Timer(60, func, [])

timer.start()

timer = threading.Timer(1, func, [])

timer.start()

3.发邮件,将图片作为附件

from email.mime.text import MIMEText

from email.header import Header

from smtplib import SMTP_SSL

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

def send_mail(mail_title='', mail_content='', pic_name=''):

sender = 'xx@qq.com'

receiver = 'xx@qq.com'

pwd = 'xxxxxxxxxxxx' # qq邮箱授权码

smtp = SMTP_SSL('smtp.qq.com') # ssl登录

smtp.login(sender, pwd)

msg = MIMEMultipart()

msg["Subject"] = Header(mail_title, 'utf-8')

msg["from"] = sender

msg["to"] = receiver

puretext = MIMEText('content: '+mail_content)

msg.attach(puretext)

jpgpart = MIMEApplication(open(pic_name, 'rb').read())

jpgpart.add_header('Content-Disposition', 'attachment', filename=pic_name)

msg.attach(jpgpart)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

完整代码

from datetime import datetime

import time

from PIL import ImageGrab

import os

import threading

def send_mail(mail_title='', mail_content='', pic_name=''):

from email.mime.text import MIMEText

from email.header import Header

from smtplib import SMTP_SSL

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

sender = 'xx@qq.com'

receiver = 'xx@qq.com'

pwd = 'xxxxxxxxxxxx' # qq邮箱授权码

smtp = SMTP_SSL('smtp.qq.com') # ssl登录

smtp.login(sender, pwd)

msg = MIMEMultipart()

msg["Subject"] = Header(mail_title, 'utf-8')

msg["from"] = sender

msg["to"] = receiver

puretext = MIMEText('content: '+mail_content)

msg.attach(puretext)

jpgpart = MIMEApplication(open(pic_name, 'rb').read())

jpgpart.add_header('Content-Disposition', 'attachment', filename=pic_name)

msg.attach(jpgpart)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

def func():

t = time.localtime(time.time())

min, hour, wkday = t.tm_min, t.tm_hour, t.tm_wday

# --------add tasks here--------------------------

if min in [0, 15, 30, 45]:

time_now = time.strftime('%Y%m%d-%H%M%S')

pic = ImageGrab.grab()

pic_name = time_now+'.jpg'

pic.save(pic_name)

title = 'mgso4 test monitor ' + time_now

send_mail(title, title, pic_name)

# os.remove(pic_name)

# --------tasks end----------------------------

global timer

timer = threading.Timer(60, func, [])

timer.start()

if __name__ == "__main__":

timer = threading.Timer(1, func, [])

timer.start()

PS1:去掉python程序的命令行窗口

把 *.py 文件后缀改成 *.pyw ,即可去掉运行时出现的cmd窗口,直接后台运行。

PS2:加入开机启动(Win10)

将 *.pyw 文件做一个快捷方式,放到开始菜单的启动文件夹C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp里,这样每次开机就能直接启动了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 的代码可以怎么写? 这个需要使用到python的几个库,比如说requests,beautifulsoup,定时任务库schedule。 首先你需要登录百度,获取搜索结果页面的html代码,然后使用beautifulsoup解析html代码,提取出日历截图。最后使用企业微信的API发送图片。 下面是一个简单的代码示例: ``` import requests from bs4 import BeautifulSoup import schedule import time import datetime def get_calendar_screenshot(): # 登录百度 # 获取搜索结果页面的html代码 # 使用beautifulsoup解析html代码,提取出日历截图 # 使用企业微信的API发送图片 # 每天9点执行一次 schedule.every().day.at("9:00").do(get_calendar_screenshot) while True: schedule.run_pending() time.sleep(1) ``` ### 回答2: 要实现定时百度网页搜索日历截图并发送到企业微信,我们可以使用Python编程语言来完成。 首先,我们需要安装所需的库,如selenium、webdriver、PIL(Python Imaging Library)、requests和openpyxl等。 我们可以使用selenium和webdriver来模拟打开浏览器并进行网页搜索。通过设置浏览器的选项,我们可以使其在后台运行,以免影响到其他操作。然后,我们可以使用PIL库中的方法来进行截图,并将截图保存到本地。 接下来,我们需要使用企业微信提供的API来发送截图。我们可以使用requests库发送HTTP请求,并将截图作为附件发送到指定的企业微信群。 最后,我们可以使用Python的datetime和time模块来计算当前时间,并设置定时任务。我们可以使用APScheduler库来创建定时任务,以便每天定时执行程序。 总结起来,我们可以编写一个Python程序,使用selenium进行网页搜索,使用PIL进行截图,使用requests发送截图到企业微信,并使用APScheduler设置定时任务来实现定时百度网页搜索日历截图发送到企业微信的功能。这样,我们就可以在每天指定的时间自动执行程序,完成所需的操作。 ### 回答3: 使用Python实现定时百度网页搜索并将日历截图发送到企业微信可以通过以下步骤实现: 1. 导入必要的模块:使用selenium模块实现对百度网页的自动搜索,使用pyautogui模块实现截图功能,使用itchat模块实现对企业微信的消息发送。 2. 设定定时任务:使用schedule模块设定任务的执行时间,可以使用装饰器功能实现定时执行。 3. 编写搜索和截图函数:使用selenium模块打开百度网页,并通过输入关键字进行搜索。搜索完成后,使用pyautogui模块实现截图功能,并保存到指定的文件夹。 4. 编写消息发送函数:使用itchat模块实现对企业微信的登录和消息发送功能。可以通过itchat.auto_login()函数实现登录,通过itchat.send_image()函数实现图片的发送。 5. 定时执行任务:在主函数中调用设定好的定时任务函数,设定好任务的执行时间频率。 以下是伪代码实现: ```python import schedule import time import pyautogui import itchat from selenium import webdriver def job(): # 在百度页面进行搜索 driver = webdriver.Chrome() driver.get("https://www.baidu.com") # 输入搜索关键字 search_box = driver.find_element_by_id("kw") search_box.send_keys("关键字") search_box.submit() # 等待搜索结果加载完成 time.sleep(5) # 截图并保存到指定文件夹 pyautogui.screenshot("screenshot.png") driver.close() # 发送截图到企业微信 itchat.auto_login() itchat.send_image("screenshot.png") # 设置定时任务,每天10:00执行 schedule.every().day.at("10:00").do(job) # 保持程序持续运行 while True: schedule.run_pending() time.sleep(1) ``` 通过以上步骤,就可以使用Python实现定时百度网页搜索并将截图发送到企业微信。注意需要预先安装好相应的模块,并根据实际情况修改代码中的关键字和执行时间等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值