QT 简易网页信息抓取程序模板基础代码

有些网页爬不了,只是一个简单的代码。

项目结构

NetBugBaseCode.pro

cpp 复制代码
#-------------------------------------------------
#
# Project created by QtCreator 2024-08-26T15:13:10
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = NetBugBaseCode
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

main.cpp

cpp 复制代码
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //初始化网络访问管理
    netManager = new QNetworkAccessManager(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::bugStart(QString url){
    qDebug() <<"bugStart";
    QByteArray data;
    //设置
    connect(netManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(bugReply(QNetworkReply*)));
    netManager->get(QNetworkRequest(QUrl(url)));
}

void MainWindow::bugReply(QNetworkReply *reply){
    qDebug() <<"bugReply:";

    QByteArray all = reply->readAll();

    qDebug() <<all;
    qDebug() <<ll;

    //显示在文字浏览器里text browser
    ui->textBrowser_1->clear();
    ui->textBrowser_2->clear();
    ui->textBrowser_1->setText(all.constData());
    ui->textBrowser_2->append("replyed");

    reply->deleteLater();

    //清除连接
    netManager->disconnect();
    netManager->clearConnectionCache();
    netManager->clearAccessCache();


}

//指定按钮的槽函数
void MainWindow::on_bugStart_clicked()
{
    //调用函数,加上网址
    bugStart("http://quote.eastmoney.com/sz002303.html");
}

ui

按下按钮后,等待一段时间,就出来了

相关推荐
倒流时光三十年15 小时前
第一阶段 05 · Java 客户端查询类详解(Query / SearchCriteria / Response 与复杂拼接)
java·开发语言·python
心平气和量大福大15 小时前
C#-WPF-控件-LiveChart图表-线性2(LineSeries)-数据绑定
开发语言·c#·wpf
lingran__15 小时前
C++_stack和queue和priority_queue(容器适配器)
开发语言·c++
::呵呵哒::15 小时前
java中SseEmitter
java·开发语言
星恒随风15 小时前
C++ 多态底层原理:静态绑定、动态绑定、虚函数表与工程实践
开发语言·c++·笔记·学习
wuyk55515 小时前
68.嵌入式 C 语言避坑指南:volatile 关键字 —— 单片机中断、寄存器访问的核心技巧
c语言·开发语言·stm32·单片机·嵌入式硬件
qz_Serene15 小时前
C语言:编译和链接
c语言·开发语言
会编程的土豆15 小时前
功能详解 01 · 用户注册:从表单到数据库一行
开发语言·数据库·后端·mysql·golang
Ivanqhz16 小时前
Rust collect() 浅析
开发语言·后端·rust
Wang's Blog16 小时前
Go-Zero项目开发33: IM实时通信前后端对接与微服务基础设施实践
开发语言·微服务·golang·go-zero