Python教程

python截图html并发送带图片的html邮件

本文主要是介绍python截图html并发送带图片的html邮件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

截图html

通过seleniun的webdriver打开html文件并截图

 1 import os
 2 import shutil
 3 from selenium import webdriver
 4 import time
 5 import random
 6 
 7 try:
 8     driver = webdriver.Chrome(r"E:\chromedriver.exe")          ## 自己现在并放到指定目录,需要自己修改
 9     picture_url = "https://www.baidu.com"
10 
11     driver.get(picture_url)
12     driver.maximize_window()
13 
14     print(dir(driver))
15 
16     time.sleep(1)
17 
18     driver.get_screenshot_as_file('E:\\11.jpg')
19     print("%s:截图成功!!!" % picture_url)
20     driver.close()
21 except BaseException as msg:
22     print(msg)

发送带图片的html邮件

#!/usr/bin/env python
# coding=utf-8


import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

msg_from = 'porsche_qa@126.com'  # 发送方邮箱
passwd = 'SLZCOAJIPHFGJOWB'  # 填入发送方邮箱的授权码
msg_to = 'binzichen@126.com'  # 收件人邮箱


def send():
    subject = "python邮件测试"  # 主题
    msg = MIMEMultipart('related')
    content = MIMEText('<html><body><div><img src="cid:imageid" alt="imageid" style="width: 1065px;height: 450px;"></div></body></html>', 'html', 'utf-8')  # 正文
    msg.attach(content)
    msg['Subject'] = subject
    msg['From'] = msg_from
    msg['To'] = msg_to

    file = open(r"new.png", "rb")
    img_data = file.read()
    file.close()

    img = MIMEImage(img_data)
    img.add_header('Content-ID', 'imageid')
    msg.attach(img)

    try:
        s = smtplib.SMTP_SSL("smtp.126.com", 465)  # 邮件服务器及端口号
        s.login(msg_from, passwd)
        s.sendmail(msg_from, msg_to, msg.as_string())
        print('发送成功!')
    except Exception:
        print('发送失败!')
    finally:
        s.quit()

if __name__ == '__main__':
    send()

 

这篇关于python截图html并发送带图片的html邮件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!