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

      

  • 相关阅读:
    STL容器list容器API
    STL容器之list基本概念
    STL容器queue的API
    做tab切换时,点击浏览器返回拿不到实时的tab参数,请求不到实时的数据
    v-for渲染出来的列表,要根据不同的状态改变样式,通过给标签添加lang属性完成
    清除每隔5000毫秒请求一次接口的定时器(需求:每当我手动核销电子码,页面上的显示数据要实时更新到)
    ES6数组解构赋值
    es6 var、let、const命令
    canvas计算高度(自定义高度)
    Vue 点击button请求接口,接收不到返回的参数
  • 原文地址:https://www.cnblogs.com/navysummer/p/14190098.html
Copyright © 2020-2023  润新知