QT5 WebCapture 网页定时截图工具
1.设置启动时间,程序会到启动时间后开始对网页依次进行截图
2.根据所需截图的页面加载速度,设置页面等待时间,尽量达到等页面加载完成后,再执行截图
3.根据需求,设置截图周期
4.程序会使用默认浏览器打开页面进行截图,每轮截图完成后,便会根据默认浏览器进程名称关闭浏览器(防止留存大量页面导致系统卡顿)
运行情况
项目结构
源代码
WebCapture.pro
#-------------------------------------------------
#
# Project created by QtCreator 2023-08-22T16:47:49
#
#-------------------------------------------------QT += core gui
QT +=testlibgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 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.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \mainwindow.uiRESOURCES += \icon.qrc
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QDesktopServices>
#include <QUrl>
#include <QTest>
#include <QApplication>
#include <QScreen>
#include <QPixmap>
#include <QIcon>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QRegularExpressionMatchIterator>
#include <QDateTime>
#include <QTimer>
#include <QElapsedTimer>#include<QProcess>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);void GetScreen(int count);//截取整个屏幕并保存void _CloseBrowser();void _EachTurn(QString urls);//执行每一轮截图float _WebWaitTime=20;//截图等待时间,单位为秒float _Interval=30;//截图周期,单位为分钟QString _Browser="";//默认浏览器的名称.exeQDateTime datetimeValue;//首次开始截图的时间QString log="";qint64 timestamp;~MainWindow();private slots:void on_pushButton_Start_clicked();private:Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QScreen>
#include <QGuiApplication>
#include <QPixmap>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
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()<<"原始字符串:"<<urls<<endl;//排除url重定向的链接打乱顺序 如 http://xx.xxx.xx.x/login.php?redirect=http://xxx.xx.xx/urls.replace("=http","1");//去除\r\n\turls.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()<<"处理后的字符串"<<urls;//设置为禁用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)//执行每一轮截图{//提取urlsQRegularExpression Re("(?<url>http[s]{0,1}.*?://.*?)fengefu");QRegularExpressionMatchIterator Matchs=Re.globalMatch(urls);QRegularExpressionMatch match=Matchs.next();QString oneUrl=match.captured("url");//提取每一个urlqDebug()<<"提取到"<<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;
}