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

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

相关推荐
喵了meme3 小时前
C语言实战4
c语言·开发语言
码界奇点3 小时前
Python从0到100一站式学习路线图与实战指南
开发语言·python·学习·青少年编程·贴图
9ilk3 小时前
【C++】--- 特殊类设计
开发语言·c++·后端
sali-tec3 小时前
C# 基于halcon的视觉工作流-章68 深度学习-对象检测
开发语言·算法·计算机视觉·重构·c#
生骨大头菜5 小时前
使用python实现相似图片搜索功能,并接入springcloud
开发语言·python·spring cloud·微服务
绝不收费—免费看不了了联系我5 小时前
Fastapi的单进程响应问题 和 解决方法
开发语言·后端·python·fastapi
消失的旧时光-19435 小时前
深入理解 Java 线程池(二):ThreadPoolExecutor 执行流程 + 运行状态 + ctl 原理全解析
java·开发语言
咖啡续命又一天5 小时前
Trae CN IDE 中 Python 开发的具体流程和配置总结
开发语言·ide·python·ai编程
4311媒体网5 小时前
帝国cms调用文章内容 二开基本操作
java·开发语言·php
GSDjisidi6 小时前
东京IT软件会社-(株)GSD|多种技术栈募集,高度人才+20分
开发语言·面试·职场和发展