QT第6个程序 - 网页内容摘取

httphelper.h

#ifndef HTTPHELPER_H

#define HTTPHELPER_H

#include <QtNetwork>

#include <QObject>

#include<QUrl>

#include<QString>

#include<QNetworkAccessManager>

class HttpHelper : public QObject

{

Q_OBJECT

public:

explicit HttpHelper();

~HttpHelper();

QString GetUrlData(QString url);

};

#endif // HTTPHELPER_H

httphelper.cpp

#include "httphelper.h"

#include <QtNetwork>

#include<QNetworkAccessManager>

#include<QNetworkReply>

#include<QNetworkRequest>

#include<QDebug>

#include<QList>

#include<QSslError>

HttpHelper::HttpHelper(QObject *parent)

: QObject{parent}

{

}

QString HttpHelper::GetUrlData(QString url)

{

//QString baseUrl="https://www.ziqqq.com/zidian/?q=chuāng";

QNetworkAccessManager *http =new QNetworkAccessManager(this);

connect(http,&QNetworkAccessManager::finished,this,[=](QNetworkReply *re){

qDebug()<<"http finished!!";

if(re->error())

{

qDebug()<<"http Error"<<re->errorString();

}

});

QNetworkRequest requ(QUrl(url,QUrl::StrictMode));

QSslConfiguration sslConfiguration(QSslConfiguration::defaultConfiguration());

requ.setSslConfiguration(sslConfiguration);

QNetworkReply *resp=http->get(requ);

connect(resp,&QNetworkReply::finished,this,[=](){

qDebug()<<"respReady!";

});

connect(resp,&QNetworkReply::sslErrors,this,[=](const QList<QSslError> &ers){

qDebug() << ers;

});

while (!resp->isFinished()) {

qApp->processEvents();//关键啊。

}

QString backdata=resp->readAll();

resp->disconnect();

resp->close();

resp->deleteLater();

resp= nullptr;

http->deleteResource(requ);

http->clearAccessCache();

http->clearConnectionCache();

http->deleteLater();

http=nullptr;

return backdata;

}

bcgm.h

#ifndef BCGM_H

#define BCGM_H

#include <QMainWindow>

#include<QList>

#include<QString>

#include"mllist.h"

class BCGM : public QMainWindow

{

Q_OBJECT

public:

BCGM(QWidget *parent = nullptr);

~BCGM();

QList<MLlist> GetBCGMlist(QString html);

QString GetAttText(QString one,QString obj,QString a);

QString GetText(QString one,QString obj);

QString GetHtmlText(QString one,QString skey,QString ekey);

QList<MLlist> ML;

signals:

void LoadData();

};

#endif // BCGM_H

bcgm.cpp

#include<QCheckBox>

#include<QStandardItemModel>

#include<QAction>

#include<QGroupBox>

#include<QTreeView>

#include<QLabel>

#include<QPushButton>

#include "bcgm.h"

#include<QGuiApplication>

#include<QScreen>

#include<QTextBrowser>

#include<QTextDocument>

#include"httphelper.h"

#include"mllist.h"

#include<QStringList>

#include<QString>

#include<QDebug>

BCGM::BCGM(QWidget *parent)

: QMainWindow(parent)

{

this->setFixedSize(800,480);

this->move(QGuiApplication::primaryScreen()->size().width()/2-this->width()/2,

QGuiApplication::primaryScreen()->size().height()/2-this->height()/2);

QPushButton *btn=new QPushButton("更新数据",this);

btn->resize(100,30);

btn->move(10,10);

QCheckBox *chk=new QCheckBox("同步用药说明",this);

chk->resize(100,30);

chk->move(100+10,10);

QLabel * lastate =new QLabel("",this);

lastate->resize(this->width()-20-btn->width()*2-10,30);

lastate->move(100+100+10+10,10);

lastate->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

QGroupBox *grp=new QGroupBox("本草纲目",this);

grp->resize(this->width()-20,this->height()-10-45);

grp->move(10,45);

QTreeView *tv = new QTreeView(grp);

tv->resize((grp->width()-20)*0.3,grp->height()-30);

tv->move(10,20);

QStandardItemModel *model = new QStandardItemModel();

model->setObjectName("本草纲目目录");

tv->setModel(model);

QTextBrowser *web=new QTextBrowser(grp);

web->resize((grp->width()-20)*0.7-5,grp->height()-30);

web->move(tv->x()+tv->width()+5,20);

connect(this,&BCGM::LoadData,this,[=](){

QString url="https://www.*.net/*/book_d809ec69c6fe.aspx";

HttpHelper h;

QString html=h.GetUrlData(url);

ML=GetBCGMlist(html);

tv->actions().clear();

QStringList keys;

foreach (MLlist ls, ML) {

if(keys.contains(ls.Type))

continue;

QStandardItem *rootItem = new QStandardItem(ls.Type);

rootItem->setFont(QFont("黑体",12));

foreach (MLlist ll, ML) {

if(ll.Type==ls.Type)

{

QStandardItem *chItem = new QStandardItem(ll.Title);

chItem->setStatusTip(ll.Url);

rootItem->appendRow(chItem);

}

}

model->appendRow(rootItem);

keys.append(ls.Type);

}

if(chk->checkState()==Qt::Checked)

{

int count=ML.count();

int now=1;

foreach (MLlist ml, ML)

{

ml.html=GetHtmlText(h.GetUrlData(ml.Url),"<div class=\"contson\"","</div>");

QString title= ml.Url+":"+

QString::number(ml.html.length())+" "+ QString::number(now )+ "/"

+QString::number(count) + " 完成:"+ QString::number(now*100.0/count) +"%";

lastate->setText(title);

now++;

}

}

});

connect(btn,&QPushButton::clicked,this,[=](){

emit LoadData();

});

connect(tv,&QTreeView::clicked,this,[=](QModelIndex index){

HttpHelper h;

web->setText("正在扫描信息......");

QString data=index.data().toString();

foreach (MLlist ml, ML) {

if(ml.Title==data){

if(ml.html=="")

{

ml.html=GetHtmlText(h.GetUrlData(ml.Url),"<div class=\"contson\"","</div>");

}

web->setHtml(ml.html);

return;

}

}

web->setText("没有找到数据内容!");

});

}

QList<MLlist> BCGM::GetBCGMlist(QString html)

{

QList<MLlist> ML;

QStringList data=html.split(QLatin1Char('\n'), Qt::SkipEmptyParts);

QString Bu;

MLlist last;

QString one;

for(int i=0;i<data.length();i++)

{

if(data[i]=="")

continue;

one=data[i];

if(one.contains("bookMl")>0 && one.contains("部"))

{

Bu=GetText(one,"strong");

}

else if(one.contains("<a href=\"/guwen/bookv_") && one.contains("第"))

{

if(Bu=="")

continue;

MLlist x;

x.Type=Bu;

QString title=GetText(one,"a");

x.Title=title.left(title.indexOf("第"));

x.Url="https://www.guwendao.net/"+GetAttText(one,"a","href");

x.html="";

ML.append(x);

}

}

return ML;

}

QString BCGM::GetAttText(QString one,QString obj,QString a)

{

int s=-1;

int e=-1;

s=one.indexOf("<"+obj);

if(s>=0)

{ s=one.indexOf(a+"=\"",s+1);

if(s>0)

{

s+=a.length()+2;

e=one.indexOf("\"",s+1);

if(e>s)

{

return one.mid(s+1,e-s-1);

}

}

}

return "";

}

QString BCGM::GetText(QString one,QString obj)

{

int s=-1;

int e=-1;

s=one.indexOf("<"+obj);

if(s>=0)

{ s=one.indexOf(">",s+1);

if(s>0)

{

e=one.indexOf("<",s+1);

if(e>s)

{

return one.mid(s+1,e-s-1);

}

}

}

return "";

}

QString BCGM::GetHtmlText(QString one,QString skey,QString ekey)//="<div class=\"contson\">"="</div>"

{

int s=one.indexOf(skey);

if(s>0)

{

int e=one.indexOf(ekey,s+skey.length()+1);

if(e>s)

{

return one.mid(s,e+ekey.length()-s-1);

}

}

return "";

}

BCGM::~BCGM()

{

}

mllist.h

#ifndef MLLIST_H

#define MLLIST_H

#include<QString>

class MLlist

{

public:

MLlist();

int Index;

QString Type;

QString Title;

QString Url;

QString html;

bool FindKeys(QString key);

};

#endif // MLLIST_H

mllist.cpp

#include "mllist.h"

MLlist::MLlist()

{

}

bool MLlist::FindKeys(QString key)

{

if(Title.contains(key) || html.contains(key))

return true;

return false;

}

软件效果:

内容是从网站上摘取下来的。程序现在没有保存能力。如果没有网络将看不到这些内容。

本人一惯要求精益求精:

加搜狗网的图片:

相关推荐
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1233 天前
matlab画图工具
开发语言·matlab
dustcell.3 天前
haproxy七层代理
java·开发语言·前端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054963 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月3 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237173 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian3 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡3 天前
简单工厂模式
开发语言·算法·c#