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 last edited by Josz
    #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

    Gojir4G 2 Replies Last reply
    0
    • J 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

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on 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
      4
      • J 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

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on 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
        • Gojir4G Gojir4

          @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 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
          0
          • J Josz

            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 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.

            Pablo J. RoginaP 1 Reply Last reply
            1
            • J Josz

              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. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on 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

              • Login

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