本文主要是介绍QT 简易网页信息抓取程序模板基础代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
有些网页爬不了,只是一个简单的代码。
项目结构
NetBugBaseCode.pro
#-------------------------------------------------
#
# Project created by QtCreator 2024-08-26T15:13:10
#
#-------------------------------------------------QT += core gui networkgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 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.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \mainwindow.ui
main.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
#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 browserui->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
按下按钮后,等待一段时间,就出来了
这篇关于QT 简易网页信息抓取程序模板基础代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!