QT5 WebCapture 页面定时截图工具

news2024/9/29 11:30:25

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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1073280.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于多线程的Reactor模式的 回声服务器 EchoServer

记录下 一个线程专门用来接受accept获取客户端的fd 获取fd之后 从剩余的执行线程中 找到一个连接客户端数量最少的线程 然后将客户端的fd加入到这个线程中并通过EPOLL监听这个fd 线程之间通过eventfd来通信 将客户端的fd传到 对应的线程中 参考了MediaServer 引入…

[java基础学习]之DOS命令

#java基础学习 1.常用的DOS命令&#xff1a; dir:列出当前目录下的文件以及文件夹 md: 创建目录 rd:删除目录cd:进入指定目录 cd.. :退回到上级目录 cd\ : 退回到根目录 del:删除文件 exit:退出dos命令行 1.dir:列出当前目录下的文件以及文件夹 2.md: 创建目录 …

解决Adobe Premiere Pro CC 2018打开无反应,并出现.crash的文件问题

一 问题描述 Adobe Premiere Pro CC 2018软件安装完成后&#xff0c;打开该软件没反应&#xff0c;且打开时桌面会出现Crash文件&#xff01; 二 解决方法 如果Adobe Premiere Pro CC 2018在打开时无反应&#xff0c;并出现.crash文件的问题&#xff0c;可以尝试以下解决方法…

KekeBlog项目实战(更新中)

一、前言 1. 项目简介 本项目是前后端分离项目&#xff0c;而我们所做的只有完整的后端开发工作&#xff0c;前端已经写好&#xff0c;故不做任何开发&#xff0c;仅开发后端。项目包含完整的后端中前台和后台的代码编写 前端项目下载链接&#xff1a; https://pan.baidu.c…

Git仓库迁移记录

背景&#xff1a;gitlab私服上面&#xff0c;使用 import project的方式&#xff0c;从旧项目迁移到新地址仓库&#xff0c;但是代码一直没拉过去。所以使用命令的方式&#xff0c;进行代码迁移。 第一步&#xff1a;使用git clone --mirror git地址&#xff0c;进行代码克隆 …

建立数据科学基础设施的绝佳指南 数据工程师都该人手一册

《Effective数据科学基础设施》由Netflix工程师Ville Tuulos撰写&#xff0c;以Metaflow为对象&#xff0c;介绍了数据科学所需要的基础设施&#xff0c;囊括数据准备、特征工程、模型训练、模型部署、服务和持续监控等环节。Metaflow专注于构建生产流程&#xff0c;更适合具有…

《理解深度学习》2023最新版本+习题答案册pdf

刚入门深度学习或者觉得学起来很困难的同学看过来了&#xff0c;今天分享的这本深度学习教科书绝对适合你。 就是这本已在外网获13.1万次下载的宝藏教科书《理解深度学习》。本书由巴斯大学计算机科学教授Simon J.D. Prince撰写&#xff0c;全书共541页&#xff0c;目前共有21…

【嵌入式C内存管理】

记录嵌入式C内存划分&#xff0c;后续会更新动态内存管理 1. 内存划分 栈区 stack有时也称为堆栈&#xff0c;重点在栈字&#xff0c;存放函数内部临时变量堆区 heap也就是动态申请&#xff08;malloc&#xff09;、释放(free)的内存区域数据区 data初始化的全局变量和静态变量…

opengauss数据备份(docker中备份)

首先如果想直接在宿主机上进行使用gs_dump备份需要glibc的版本到2.34及以上&#xff0c;查看版本命令为 ldd --version 如图所示&#xff0c;本宿主机并不满足要求&#xff0c;所以转向在docker容器中进行备份&#xff0c; 然后进入opengauss容器中&#xff0c;命令为 docker…

Vue3 + Ts实现NPM插件 - 定制loading

目录 你的 Loading&#x1f916; 安装&#x1f6f9; 简介苍白请 您移步文档&#xff1a;✈️ 使用方法&#x1f6e0;️ 配置 loading 类型&#x1f3b2; 定制 loading 色彩 &#x1f4a1; 注意事项 前期回顾 你的 Loading 开箱即可用的 loading&#xff0c; 说明&#xff1a;vu…

当数字孪生与智慧园区结合,能够实现什么样的应用?

随着数字化进程的加深&#xff0c;数字孪生技术也越来越为大家所重视。那么&#xff0c;数字孪生技术在智慧园区中能够发挥什么样的作用&#xff1f;本文将根据山海鲸可视化智慧园区三维可视化系统&#xff0c;为大家进行说明。 一、基本概念 为了方便大家了解&#xff0c;这…

基于Springboot实现学生毕业离校系统项目【项目源码+论文说明】分享

基于Springboot实现学生毕业离校系统演示 摘要 随着科学技术的飞速发展&#xff0c;社会的方方面面、各行各业都在努力与现代的先进技术接轨&#xff0c;通过科技手段来提高自身的优势&#xff0c;新生宿舍管理系统当然也不能排除在外。新生宿舍管理系统是以实际运用为开发背景…

使用echarts绘制3DChart图表

使用3DChart需要安装echarts和echarts-gl。否则图标不显示。 版本要对应 “echarts”: “^5.2.2”, “echarts-gl”: “^2.0.9”, main.js // main.js 引入echarts方式如下 import echarts-gl //如果使用3DEchart图标需要下载个引入对应版本的 import * as echarts from echa…

旅游网站HTML

代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>旅游网</title> </head> <body><!--采用table编辑--> <!--最晚曾table,用于整个页面那布局--><table width&q…

【星海出品】ansible入门(四)playbook kolla开源例子

简介 Kolla-ansible项目提供一个完整的Ansible Playbook&#xff0c;来部署Docker的镜像&#xff0c;再完成openstack组件的自动化部署。并提供all-in-one和multihost的环境。 安装后会将kolla-ansible内置为一个shell启动文件。 kolla-ansible: /usr/local/bin/kolla-ansible…

获取手机号归属地详情,精准高效的API接口服务

在现代社会中&#xff0c;通讯工具如手机成为了人们生活中不可缺少的部分。但是&#xff0c;有时我们会收到陌生电话&#xff0c;需要了解电话号码的归属地以判断其可信性。这个时候&#xff0c;获取手机号归属地的API接口服务就会发挥重要作用。 一、API接口服务简介 API接口…

解决video层级过高在app的问题

直接上代码,写一个组件 <template><iframe :onload"inner"></iframe> </template> <script>export default {props: {src: {}},data() {return {inner: }},created() {this.inner this.contentWindow.document.body.innerHTML <v…

一文搞懂二叉树后序遍历的三种方法

系列文章&#xff1a; 相关题目&#xff1a; 145. 二叉树的后序遍历 先序遍历结果为&#xff1a;4 5 2 6 7 3 1 总体上分为两种框架&#xff0c;递归框架和非递归框架&#xff0c;递归框架又分为两种思路&#xff1a;分解思路和遍历思路。 递归 1、分解思路 【分解为子问题】…

直线模组的应用场景

直线模组是一种由直线导轨、滑块、驱动部件等组成的直线运动系统&#xff0c;具有高精度、高速度、高效率等特点。直线模组被广泛应用于各种机械设备中&#xff0c;以下是其主要的应用场景&#xff1a; 1、数控机床&#xff1a;直线模组是数控机床中的重要组成部分&#xff0c;…

过滤器的实现及其原理责任链设计模式

Filter过滤器 过滤器的应用 DeptServlet,EmpServlet,OrderServlet三个业务类的业务方法执行之前都需要编写判断用户是否登录和解决的中文乱码的代码,代码没有得到重复利用 Filter是过滤器可以用来编写请求的过滤规则和多个Servlet都会执行的公共代码,Filter中的业务代码既可…