• QT5 WebCapture 页面定时截图工具


    QT5 WebCapture 网页定时截图工具

    1.设置启动时间,程序会到启动时间后开始对网页依次进行截图
    2.根据所需截图的页面加载速度,设置页面等待时间,尽量达到等页面加载完成后,再执行截图
    3.根据需求,设置截图周期
    4.程序会使用默认浏览器打开页面进行截图,每轮截图完成后,便会根据默认浏览器进程名称关闭浏览器(防止留存大量页面导致系统卡顿)

    运行情况

    在这里插入图片描述

    项目结构

    在这里插入图片描述

    源代码

    WebCapture.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2023-08-22T16:47:49
    #
    #-------------------------------------------------
    
    QT += core gui
    QT +=testlib
    
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = WebCapture
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    
    SOURCES += \
            main.cpp \
            mainwindow.cpp
    
    HEADERS += \
            mainwindow.h
    
    FORMS += \
            mainwindow.ui
    
    RESOURCES += \
        icon.qrc
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        void GetScreen(int count);//截取整个屏幕并保存
        void _CloseBrowser();
        void _EachTurn(QString urls);//执行每一轮截图
        float _WebWaitTime=20;//截图等待时间,单位为秒
        float _Interval=30;//截图周期,单位为分钟
        QString _Browser="";//默认浏览器的名称.exe
        QDateTime datetimeValue;//首次开始截图的时间
        QString log="";
        qint64 timestamp;
        ~MainWindow();
    
    private slots:
        void on_pushButton_Start_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    main.cpp

    #include "mainwindow.h"
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QDateTime currentDateTime = QDateTime::currentDateTime();
        ui->dateTimeEdit_StartTime->setDisplayFormat("yyyy/MM/dd HH:mm:ss");
        ui->dateTimeEdit_StartTime->setDateTime(currentDateTime);
        //固定窗体大小,
            this->setMaximumSize(this->width(),this->height());
            this->setMinimumSize(this->width(),this->height());
            //隐藏最大化按钮
            this->setWindowFlag(Qt::WindowMaximizeButtonHint,0);
            this->setWindowTitle("定时截图工具-半个橘子");
            this->setWindowIcon(QIcon(":/myicon/bgjzicon.png"));
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_Start_clicked()
    {
         //这一段是用来匹配出每一个url,可以增加一些对输入格式的兼容性...........................................................
            QString urls=ui->plainTextEdit_urls->toPlainText();
            //qDebug()<<"原始字符串:"<
            //排除url重定向的链接打乱顺序 如 http://xx.xxx.xx.x/login.php?redirect=http://xxx.xx.xx/
            urls.replace("=http","1");
            //去除\r\n\t
            urls.remove("\n"); urls.remove("\r"); urls.remove("\t");urls.remove(" ");
            //这样可以使用fengefu有效分割出每个url,适应不同的输入格式
            urls.replace("http://","fengefuhttp://");
            urls.replace("https://","fengefuhttps://");
            //在末尾加上分隔符这样可以兼容最后一个url,使得最后一个url得到匹配
            urls=urls+"fengefu";
            //qDebug()<<"处理后的字符串"<
    
            //设置为禁用
            ui->pushButton_Start->setDisabled(true);
            ui->plainTextEdit_urls->setDisabled(true);
            ui->dateTimeEdit_StartTime->setDisabled(true);
            ui->lineEdit_interval->setDisabled(true);
            ui->lineEdit_WebWaitTime->setDisabled(true);
            ui->lineEdit_browser->setDisabled(true);
    
            ui->pushButton_Start->setText("进行中...");
            this->log="";
            //获取单个页面等待时间,默认占位填写的是10秒
            this->_WebWaitTime=ui->lineEdit_WebWaitTime->text().toFloat();
            //获取截图周期,默认占位写的是30分钟
            this->_Interval=ui->lineEdit_interval->text().toFloat();
            qDebug()<<"截图周期"<<this->_Interval<<"分钟"<<endl;
            //获取浏览器进程名称
            this->_Browser=ui->lineEdit_browser->text();
    
            //获取首次开始时间
            this->datetimeValue=ui->dateTimeEdit_StartTime->dateTime();
            //转换为时间戳
            this->timestamp = datetimeValue.toMSecsSinceEpoch();
            this->log=this->log+"本轮截图将于"+datetimeValue.toString("yyyy-MM-dd HH:mm:ss")+"开始,图片将保存至ScreenPicture文件夹内\n";
            ui->plainTextEdit_log->setPlainText(this->log);
    
           //时间到的时候,自动进行一轮截图
           QTimer *timer = new QTimer(this);
           connect(timer, &QTimer::timeout, this, [=]()
           {
               QDateTime currentTime = QDateTime::currentDateTime();
               if(currentTime.toMSecsSinceEpoch() >= this->timestamp)
               {
                   _EachTurn(urls); // 执行一轮截图
    
                   // 计算下次开始时间
                   this->datetimeValue = this->datetimeValue.addSecs(this->_Interval*60);
                   this->timestamp = this->datetimeValue.toMSecsSinceEpoch(); // 下次执行时间的时间戳
                   this->log = this->log + "本轮截图将于" + datetimeValue.toString("yyyy-MM-dd HH:mm:ss") + "开始,图片将保存至ScreenPicture文件夹内\n";
                   ui->plainTextEdit_log->setPlainText(this->log);
               }
           });
           timer->start(1000); // 每1000毫秒(1秒)触发一次
    
    
    }
    
    
     void MainWindow::_EachTurn(QString urls)//执行每一轮截图
     {
         //提取urls
    
            QRegularExpression Re("(?http[s]{0,1}.*?://.*?)fengefu");
    
            QRegularExpressionMatchIterator Matchs=Re.globalMatch(urls);
            QRegularExpressionMatch match=Matchs.next();
            QString oneUrl=match.captured("url");//提取每一个url
            qDebug()<<"提取到"<<oneUrl<<endl;
            QDesktopServices::openUrl(QUrl(oneUrl));
            QTest::qSleep(this->_WebWaitTime*1500);//等到完全加载好,乘1500是多加载一会,因为对于第一个页面正则和打开浏览器都消耗了时间
    
            int count=1;
            this->GetScreen(count);//截图保存
    
            while(Matchs.hasNext()==true)
           {
               QTest::qSleep(2000);//等一会再打开下一个网页,不然会截错
               match=Matchs.next();
               oneUrl=match.captured("url");
               qDebug()<<"提取到"<<oneUrl<<endl;
               // 打开一个网页
               QDesktopServices::openUrl(QUrl(oneUrl));
               QTest::qSleep(this->_WebWaitTime*1000);//等到一会,到页面完全加载好
               count++;
               this->GetScreen(count);//截图保存
           }
    
             this->_CloseBrowser();//完成本轮截图,关闭打开的默认浏览器
             this->log=this->log+"———截图完成———\n\n";
    
     }
    
    
    void MainWindow::GetScreen(int count)//截取整个屏幕并保存
    {
        //截取整个屏幕并保存
        QScreen *screen = QApplication::primaryScreen();
        QPixmap originalPixmap = screen->grabWindow(0);
        // 将时间格式化为字符串并输出
        QString timeString = this->datetimeValue.toString("yyyy年MM月dd日-HH时mm分");
        QString picname="ScreenPicture/"+timeString+"-"+QString::number(count)+".png";
        originalPixmap.save(picname);
    }
    
    
    void MainWindow::_CloseBrowser()//关闭默认浏览器
    {
        //执行cmd命令
        QProcess p(0);
        QString command="taskkill /im "+this->_Browser+" /f";
        p.start("cmd", QStringList()<<"/c"<<command);
        p.waitForFinished();
        QString strTemp=QString::fromLocal8Bit(p.readAllStandardOutput());
        qDebug()<<strTemp;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
  • 相关阅读:
    提升企业网络安全的重要性:EventLog Analyzer的角色
    亚马逊、Shopee、美客多店铺出单量如何提高?有何方法?
    Leetcode第150题—逆波兰表达式
    【笔记】《结网:互联网产品经理改变世界》
    图片怎么压缩大小?这样压缩图片很简单
    mybatis拦截器实现数据脱敏&拦截器使用
    YOLOv8-Cls推理详解及部署实现
    SpringBoot+jSerialComm实现Java串口通信 读取串口数据以及发送数据
    Vue08 事件的基本使用
    论文翻译 | ITER-RETGEN:利用迭代检索生成协同增强检索增强的大型语言模型
  • 原文地址:https://blog.csdn.net/Alsn86/article/details/133683780