前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >提取多个html生成单个html

提取多个html生成单个html

作者头像
懿曲折扇情
发布2022-08-24 13:01:53
1.1K0
发布2022-08-24 13:01:53
举报
文章被收录于专栏:建帅技术分享
代码语言:javascript
复制
#!/bin/bash
# coding=utf-8
"""
    作者:gaojs
    功能:
    新增功能:
            schedule 是一个第三方轻量级的任务调度模块,可以按照秒,分,小时,日期或者自定义事件执行时间。
            schedule 允许用户使用简单、人性化的语法以预定的时间间隔定期运行 Python 函数(或其它可调用函数)
        demo如下:
            schedule.every(10).seconds.do(job)
            schedule.every(10).minutes.do(job)
            schedule.every().hour.do(job)
            schedule.every().day.at("10:30").do(job)
            schedule.every(5).to(10).minutes.do(job)
            schedule.every().monday.do(job)
            schedule.every().wednesday.at("13:15").do(job)
            schedule.every().minute.at(":17").do(job)
    日期:2022/5/29 12:45
    blog: www.gaojs.com.cn
"""
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP_SSL
from email.header import Header
import schedule
from selenium import webdriver
from email.mime.text import MIMEText
from selenium.webdriver.firefox.options import Options
import re
import time
import os



def get_report_source_code():
    """
    获取test报告源码页面
    """
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    driver = webdriver.Firefox(options=chrome_options)
    release_name = ['APV', 'netIAG', 'FT', 'UAG', 'AG9.4']
    source = ''
    for release in release_name:
        time.sleep(5)
        url = f'http://192.168.120.204/builds/{release}/'
        driver.get(url)
        driver.maximize_window()
        source_code = driver.page_source
        # 删除替换网页源码部分标签
        result = re.sub('<td valign="top">(.*?)</td>', ' ', source_code)
        result = re.sub('<tr> <td><a href="/builds/">Parent(.*?)</td></tr>', ' ', result)
        result = re.sub('<address>(.*?)</address>', ' ', result)
        result = re.sub('<h1>(.*?)</h1>', f'<h1>{release} Latest Builds</h1>', result)
        result1 = re.sub('<tr><th valign="top">(.*?)</tr>', ' ', result)
        r = re.findall('<tr> <td><a href=".*?">(.*?)</a></td>', result1)
        if r:
            s1 = result1
            for i in r:
                t = f'http://192.168.120.204/builds/{release}/' + i
                s0 = re.sub(f'<tr> <td><a href=".*?">{i}</a></td>', f'<tr> <td><a href=".*?">{t}</a></td>', s1)
                s1 = s0
            source = source + s1
    driver.close()
    return source


def send_email():
    """
    发送test_report邮件
    """
    # 获取页面源码
    source_code = get_report_source_code()
    # 以126邮箱为例
    # ----------------发件相关参数----------------
    smtpserver = 'smtp.126.com'
    port = 0
    sender = 'testops_jianshuai@126.com'
    password = 'HHHAXZQYIAPTAQST'
    receicer = ['gaojs@arraynetworks.com.cn', '1315202xxxx@163.com']

    # ----------------编辑邮件内容----------------
    subject = '204 server 最新builds 一览表'
    body = f'<p>{source_code}<p>'
    msg = MIMEMultipart()
    msg['Subject'] = Header(subject, 'utf-8')  # 邮件主题
    msg['From'] = sender  # 发件人
    msg['To'] = ';'.join(receicer)
    msg.attach(MIMEText(body, 'html', 'utf-8'))
    attchment = MIMEApplication(open(r'./report.html', 'rb').read())
    attchment.add_header('Content-Disposition', 'attachment', filename="report.html")
    msg.attach(attchment)  # 添加附件到邮件

    smtp = SMTP_SSL(smtpserver)
    smtp.login('testops_jianshuai@126.com', password)
    smtp.sendmail(sender, receicer, msg.as_string())
    smtp.quit()
    print('******************* 邮件发送完成,请查收附件! ************************')


def download():
    """
    自动下载release
    """
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    driver = webdriver.Firefox(options=chrome_options)
    print('*************************** 正在刷新新版本,请稍等 *******************************')
    url = 'http://10.3.0.20/sp2/build/rel_ag_9_4.html'
    time.sleep(5)
    driver.get(url)
    driver.maximize_window()
    print('downloading')
    start_time = time.time()
    try:
        element = driver.find_element_by_xpath(xpath='/html/body/blockquote[1]/a[1]').text
        release_url = element.split(' ')[0]
        print(release_url)
        # 分离url和文件名称
        release_name = os.path.split(release_url)[1]
        print(release_name)
    finally:
        os.chdir('/var/www/html/builds/AG9.4/')
        if not os.path.exists('/var/www/html/builds/AG9.4/' + release_name):
            os.system(r'curl -o %s  %s' % (release_name, release_url))
    end_time = time.time()
    driver.close()
    print(f'*************************** 总共用时 {end_time - start_time} s **************************')


if __name__ == '__main__':
    send_email()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档