当前位置: 首页 > news >正文

selenium无头浏览器截图并以邮件发送

  • selenium
    4.11版本的selenium无需在本地下载浏览器驱动
# @Time    : 2024/10/18 17:54
# @Author  : 
# @FileName: print_sc.py
# @Description:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import base64from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Servicechrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')path = r'D:\chrome_driver'
service = Service(executable_path=path)def download(url, pngName):# 设置chrome参数browser = webdriver.Chrome(options=chrome_options)try:# 获取网页内容browser.get(url)# 通过执行脚本,设置滚动条到最大宽度及最大高度width = browser.execute_script("return document.documentElement.scrollWidth")height = browser.execute_script("return document.documentElement.scrollHeight")browser.set_window_size(width, height)# 是否需要超时等待# time.sleep(10)# 保存的截图名字browser.save_screenshot(pngName)browser.quit()except Exception as err:print(err)browser.quit()finally:browser.quit()def send_email(smtp_server, port, username, password, from_addr, to_addr, subject, body, image_path):# 创建MIME多部分消息msg = MIMEMultipart('related')msg['From'] = from_addrmsg['To'] = to_addrmsg['Subject'] = subject# 创建邮件主体HTML内容# cid:image1 用于指定嵌入图像的位置html_content = f"""<html><body><p>{body}</p><img src="cid:image1" alt="Embedded Image"></body></html>"""# 添加HTML到邮件msg.attach(MIMEText(html_content, 'html'))# 读取和编码图像with open(image_path, 'rb') as img_file:img_data = img_file.read()# 创建MIMEImage对象并设置相关头信息image = MIMEImage(img_data, name=image_path.split('/')[-1])image.add_header('Content-ID', '<image1>')  # cid标识符# 将图片对象附加到邮件msg.attach(image)# 连接到SMTP服务器并发送邮件try:server = smtplib.SMTP(smtp_server, port)# server.starttls()  # 启用TLS加密# server.login(username, password)server.send_message(msg)print("Email sent successfully")except Exception as e:print(f"Failed to send email: {e}")finally:server.quit()smtp_server = 'SMTp.test.com'  # 替换为你的SMTP服务器
port = 25  # 通常为587或465,为SMTP服务器提供
username = 'xxxxx'
password = 'xxxxx'
from_addr = 'Test@test.com'if __name__ == '__main__':url = 'https://console.cn/platform'pngPath = 'D:/chrome_driver/'pngName = pngPath + "summary.png"download(url, pngName)to_addr = 'test@test.com'subject = 'Here is your embedded image'body = 'Please see the image below.'image_path = pngNamesend_email(smtp_server, port, username, password, from_addr, to_addr, subject, body, image_path)

http://www.mrgr.cn/news/62838.html

相关文章:

  • Redis-发布/订阅交互模式
  • 在面试了些外包以后,我有了些自己的思考
  • SASS转换成CSS步骤
  • 硬核解读Stable Diffusion(完整版)
  • 力扣-最小覆盖子串
  • [Vue warn]: Do not use built-in or reserved HTML elements as component id:
  • 【设计模式】如何用C++实现依赖倒置
  • AcWing 1069 凸多边形的划分 区间dp + 高精度
  • 普通人的核心竞争力
  • Vim的配置
  • 杭州E类人才认定流程
  • C++设计模式结构型模式———桥接模式
  • 排序
  • 第十五章数据管理成熟度评估
  • 新160个crackme - 088-[KFC]fish‘s CrackMe
  • Telegram bot教程:通过BotFather设置Telegram bot的命令菜单
  • Java Executor ScheduledThreadPoolExecutor 总结
  • DBeaver如何查看ER图
  • Python定义与调用函数
  • 【AI时代】普通程序员想投身AI大模型行业,该如何快速入局
  • DAY67WEB 攻防-Java 安全JNDIRMILDAP五大不安全组件RCE 执行不出网
  • 服务器宝塔安装哪吒监控
  • 数据结构(8.5_1)——归并排序
  • 通过QAxObject关闭已经打开的指定名称的Word文档
  • 【安装配置教程】一、windows安装并配置java8
  • RabbitMQ怎么保障消息的可靠性