Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Load a Web source code into a String or QString to parse

    Qt WebKit
    3
    6
    625
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      Josz last edited by Josz

      Hello, (please any help will be wellcomed)
      I can display a web with the next commands

      QWebView *view = new QWebView(nullptr);
      view->load(QUrl("https://www.elmundo.es/espana.html"));
      view->show();
      

      But how can I get the source code into a string to analyze and parse?

      Thanks in advance

      Gojir4 2 Replies Last reply Reply Quote 0
      • Gojir4
        Gojir4 @Josz last edited by

        @josz Hi,

        Using QNetworkAccessManager:

        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
        connect(manager, &QNetworkAccessManager::finished,
                this, &MyClass::replyFinished);
        
        manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
        
        MyClass::replyFinished(QNetworkReply *reply){
            if(QNetworkReply::NoError == reply.error())
                QString html = reply.readAll();
            else //Error
        }
        
        J 1 Reply Last reply Reply Quote 4
        • Gojir4
          Gojir4 @Josz last edited by

          @josz It seems (see this post) you can also get the html code using QWebFrame::toHtml()

          QString html = view ->page()->currentFrame()->toHtml();
          
          1 Reply Last reply Reply Quote 4
          • J
            Josz @Gojir4 last edited by

            Hi @gojir4 thank you , I used the next code, but didn't work and I don't know why
            source : https://stackoverflow.com/questions/1053099/how-can-i-get-content-of-web-page

            class MyClass : public QObject
            {
            Q_OBJECT
            
            public:
                MyClass();
                void fetch(); 
            
            public slots:
                void replyFinished(QNetworkReply*);
            
            private:
                QNetworkAccessManager* m_manager;
            };
            
            
            MyClass::MyClass()
            {
                m_manager = new QNetworkAccessManager(this);
            
                connect(m_manager, SIGNAL(finished(QNetworkReply*)),
                     this, SLOT(replyFinished(QNetworkReply*)));
            
            }
            
            void MyClass::fetch()
            {
                m_manager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));
            }
            
            void MyClass::replyFinished(QNetworkReply* pReply)
            {
            
                QByteArray data=pReply->readAll();
                QString str(data);
            
                //process str any way you like!
               qDebug() << data;
            }
            
            J 1 Reply Last reply Reply Quote 0
            • J
              Josz @Josz last edited by

              Agghhh. I had the QApplication a(argc, argv); and return a.exec(); commented. So, it was not an event loop.

              Sorry (and thank you very much).

              It worked.

              Pablo J. Rogina 1 Reply Last reply Reply Quote 1
              • Pablo J. Rogina
                Pablo J. Rogina @Josz last edited by

                @josz said in Load a Web source code into a String or QString to parse:

                It worked.

                Great. Please don't forget to mark your post as such! Thanks.

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post