• 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();
    }
    

      

  • 相关阅读:
    iOS 中使用 XIB 自定义cell的两种方法以及编译出现常见 的错误 (xcode6.0之后)
    iOS中 学会如何对sqlite3 进行封装
    杭电ACM2012素数判定
    杭电ACM2503a/b+c/d
    杭电ACM2005第几天
    杭电ACM2034人见人爱AB
    杭电ACM2502月之数
    杭电ACM2001计算两点间的距离
    杭电ACM2002计算求得体积
    杭电ACM2033人见人爱A+B
  • 原文地址:https://www.cnblogs.com/navysummer/p/14190098.html
Copyright © 2020-2023  润新知