Qt基于QWebEngineView的爬虫

1WebPage.pro文件

QT       += core gui webenginewidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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 
    webpage.cpp
HEADERS += 
    webpage.h
FORMS += 
    webpage.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

2.webpage.h文件

#ifndef WEBPAGE_H
#define WEBPAGE_H

#include <QWidget>
#include<QWebEngineView>
#include<QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class WebPage; }
QT_END_NAMESPACE

class WebPage : public QWidget
{
    Q_OBJECT

public:
    WebPage(QWidget *parent = nullptr);
    ~WebPage();

private slots:
    void getPage(bool);
    void getHtml(const QString&);

protected:
    void resizeEvent(QResizeEvent *);

signals:
    void html(const QString& result);

private:
    Ui::WebPage *ui;
    QWebEngineView* view;
    QWebEnginePage *page;
};
#endif // WEBPAGE_H

3.webpage.cpp文件

#include "webpage.h"
#include "ui_webpage.h"

WebPage::WebPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::WebPage)
{
    ui->setupUi(this);
    connect(this,SIGNAL(html(const QString&)),this,SLOT(getHtml(const QString&)));
    view = new QWebEngineView(this);
    view->load(QUrl("https://so.iqiyi.com/so/q_你好"));
    page = view->page();
    connect(page,SIGNAL(loadFinished(bool)),this,SLOT(getPage(bool)));
//    view->show();
}

WebPage::~WebPage()
{
    delete ui;
}

void WebPage::resizeEvent(QResizeEvent *)
{
    view->resize(this->size());
}

void WebPage::getPage(bool status){
    qWarning()<<status<<endl;
    page->toHtml([this](const QString& result) mutable {emit html(result);});
}

void WebPage::getHtml(const QString& reshtml){
    qWarning()<<reshtml<<endl;
}

 4.main.cpp

#include "webpage.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    WebPage w;
    w.show();
    return a.exec();
}

  

原文地址:https://www.cnblogs.com/navysummer/p/14190098.html