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

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

相关推荐
czq_26867194873 小时前
Python打卡第46天
开发语言·python
宛 禾5 小时前
Qt 事件系统完全指南:从事件循环到实战避坑
qt
Evand J6 小时前
【MATLAB例程介绍】(17)永磁同步电机(PMSM)基于自适应无迹卡尔曼滤波(AUKF)的电流噪声在线估计。测量噪声协方差自适应与转速估计对比仿真
开发语言·matlab·卡尔曼滤波·无迹卡尔曼滤波·pmsm·电机滤波
BaiPeter7 小时前
ZPL文件预览
开发语言·c++
噢,我明白了12 小时前
JAVA中的一些基础应用知识点
java·开发语言
时间的拾荒人13 小时前
Redis 入门详解:核心特性、重要文件与全局命令实战
开发语言·redis·面试
敲上瘾13 小时前
Qt Widgets入门:项目模板解析与Hello World
开发语言·数据库·c++·qt·ui·qt6.3
码途行者14 小时前
网站的SQL 注入:从攻击原理到防御措施
开发语言·sql·php
旖旎夜光15 小时前
【C++ 基础入门】C++ 多态(中):虚函数表、多态原理与动静绑定
开发语言·c++·笔记·学习
你看我名字15 小时前
产品参数昨天还对,今天已过期:数眼智能与大模型应用如何做资料版本校验
开发语言·架构