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

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

相关推荐
q567315236 分钟前
Go语言多线程爬虫与代理IP反爬
开发语言·爬虫·tcp/ip·golang
Chandler249 分钟前
Go语言即时通讯系统 开发日志day1
开发语言·后端·golang
强化学习与机器人控制仿真40 分钟前
openpi 入门教程
开发语言·人工智能·python·深度学习·神经网络·机器人·自动驾驶
明月看潮生1 小时前
青少年编程与数学 02-019 Rust 编程基础 08课题、字面量、运算符和表达式
开发语言·青少年编程·rust·编程与数学
天天打码2 小时前
Rspack:字节跳动自研 Web 构建工具-基于 Rust打造高性能前端工具链
开发语言·前端·javascript·rust·开源
Petrichorzncu2 小时前
Lua再学习
开发语言·学习·lua
AA-代码批发V哥2 小时前
正则表达式: 从基础到进阶的语法指南
java·开发语言·javascript·python·正则表达式
charlie1145141912 小时前
逐步理解Qt信号与槽机制
数据库·qt
炯哈哈2 小时前
【上位机——MFC】序列化机制
开发语言·c++·mfc·上位机
蓝莓味柯基3 小时前
Python3正则表达式:字符串魔法师的指南[特殊字符]‍♂️
开发语言·python·正则表达式