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

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

相关推荐
王伯安呢22 分钟前
Java开发环境配置入门指南
java·开发语言·jvm·eclipse·环境搭建·新手
·前路漫漫亦灿灿24 分钟前
C++-类型转换
开发语言·c++
Kyln.Wu40 分钟前
【python实用小脚本-205】[HR揭秘]手工党逐行查Bug的终结者|Python版代码质量“CT机”加速器(建议收藏)
开发语言·python·bug
计算机毕业设计木哥42 分钟前
Python毕业设计推荐:基于Django的饮食计划推荐与交流分享平台 饮食健康系统 健康食谱计划系统
开发语言·hadoop·spring boot·后端·python·django·课程设计
rockmelodies1 小时前
Java安全体系深度研究:技术演进与攻防实践
java·开发语言·安全
OEC小胖胖1 小时前
Next.js 介绍:为什么选择它来构建你的下一个 Web 应用?
开发语言·前端·web·next.js
代码栈上的思考1 小时前
深入解析 Java 内存可见性问题:从现象到 volatile 解决方案
java·开发语言
坐吃山猪6 小时前
SpringBoot01-配置文件
java·开发语言
晚风(●•σ )7 小时前
C++语言程序设计——06 字符串
开发语言·c++
我叫汪枫7 小时前
《Java餐厅的待客之道:BIO, NIO, AIO三种服务模式的进化》
java·开发语言·nio