Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. Load a Web source code into a String or QString to parse

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

Scheduled Pinned Locked Moved Solved Qt WebKit
6 Posts 3 Posters 1.2k Views
  • 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 Offline
    J Offline
    Josz
    wrote on 10 Sept 2019, 12:25 last edited by Josz 9 Oct 2019, 12:29
    #1

    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

    G 2 Replies Last reply 10 Sept 2019, 12:55
    0
    • J Josz
      10 Sept 2019, 12:25

      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

      G Offline
      G Offline
      Gojir4
      wrote on 10 Sept 2019, 12:55 last edited by
      #2

      @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 10 Sept 2019, 13:01
      4
      • J Josz
        10 Sept 2019, 12:25

        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

        G Offline
        G Offline
        Gojir4
        wrote on 10 Sept 2019, 13:00 last edited by
        #3

        @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
        4
        • G Gojir4
          10 Sept 2019, 12:55

          @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 Offline
          J Offline
          Josz
          wrote on 10 Sept 2019, 13:01 last edited by
          #4

          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 10 Sept 2019, 13:13
          0
          • J Josz
            10 Sept 2019, 13:01

            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 Offline
            J Offline
            Josz
            wrote on 10 Sept 2019, 13:13 last edited by
            #5

            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.

            P 1 Reply Last reply 10 Sept 2019, 13:26
            1
            • J Josz
              10 Sept 2019, 13:13

              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.

              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 10 Sept 2019, 13:26 last edited by
              #6

              @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
              1

              6/6

              10 Sept 2019, 13:26

              • Login

              • Login or register to search.
              6 out of 6
              • First post
                6/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved