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
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_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
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\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()<<"处理后的字符串"<<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)//执行每一轮截图
{
//提取urls
QRegularExpression Re("(?<url>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;
}