Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read data of a website
Forum Updated to NodeBB v4.3 + New Features

Read data of a website

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 7 Posters 1.5k Views 4 Watching
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #2

    Hi

    It depends on how the server works but you can try to send a GET and see.

    #include <QDebug>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QtNetwork/qnetworkreply.h>
    
    
    void MainWindow::on_pushButton_4_clicked()
    {
    
        QEventLoop loop;
        QNetworkAccessManager nam;
        QNetworkRequest req(QUrl("http://YOURURL.com"));
        QNetworkReply *reply = nam.get(req);
        connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
        loop.exec();
        QByteArray buffer = reply->readAll();
    
        qDebug() << "what did i get" << buffer;
    
    

    and see what you get.

    Do note. this is just test code. it lacks error handling (a must have in real app) and we don't want to use
    QEventLoop but instead just hook up to signals.
    However, its just to see if you do in fact get the text string as your image shows.

    1 Reply Last reply
    2
    • L Offline
      L Offline
      Linz
      wrote on last edited by Linz
      #3

      Thanks for your help :) I get the following message:

      Execution of PAC script at "http://wpad/wpad.dat" failed: The operation could not be completed. (NSURLErrorDomain-Fehler -1003.)
      what did i get "<?xml version="1.0" encoding="UTF-8"?>\n<r>\n<f>49.988</f>\n<z> 12.06.2021 13:00:55</z>\n<p>228.6</p>\n<dt>0</dt>\n</r>\n"

      But there is my data: 49.998 12.06.2021 13:00:55 226.8 0

      Just need to find a way to filter it. That the execution failed shouldn't bother me?

      artwawA 1 Reply Last reply
      0
      • L Linz

        Thanks for your help :) I get the following message:

        Execution of PAC script at "http://wpad/wpad.dat" failed: The operation could not be completed. (NSURLErrorDomain-Fehler -1003.)
        what did i get "<?xml version="1.0" encoding="UTF-8"?>\n<r>\n<f>49.988</f>\n<z> 12.06.2021 13:00:55</z>\n<p>228.6</p>\n<dt>0</dt>\n</r>\n"

        But there is my data: 49.998 12.06.2021 13:00:55 226.8 0

        Just need to find a way to filter it. That the execution failed shouldn't bother me?

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #4

        @Linz Hi, I don't know about the error (I personally would investigate that).
        To parse XML you can use https://doc.qt.io/qt-5/qxmlstreamreader.html

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        3
        • L Offline
          L Offline
          Linz
          wrote on last edited by Linz
          #5

          I fixed the error, something with my proxy was off ^^

          Do you have an idea how I can convert the QByteArray buffer to a char? Would It make way more easy to access the values I need.

          VRoninV 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Hi,

            No need to convert anything.

            You can access the internal data using the QByteArray::data function.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • L Linz

              I fixed the error, something with my proxy was off ^^

              Do you have an idea how I can convert the QByteArray buffer to a char? Would It make way more easy to access the values I need.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #7

              @Linz said in Read data of a website:

              Do you have an idea how I can convert the QByteArray buffer to a char? Would It make way more easy to access the values I need.

              Don't do this.

              @artwaw said in Read data of a website:

              To parse XML you can use https://doc.qt.io/qt-5/qxmlstreamreader.html

              Do this!

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              4
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                I somehow missed the XML part.

                My fellows are correct, use QXmlStreamReader.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • L Offline
                  L Offline
                  Linz
                  wrote on last edited by
                  #9

                  Sorry for bothering you: I read the documentation for QXmlStreamReader, but I still have no clue how to filter the data. Maybe I am still missing the basics too much to understand what I need...

                  If I refer to the picture I want to save the first value 49.986, the second value 132.3 and the third value 0 in an array(s) of type double (to plot it later on).

                  But which function could do this for me?
                  I tried something like:
                  void QXmlStreamReader::addData(const QByteArray &buffer);

                  but it doesn't work. I could filter it easily if it would have been an array of char :/

                  JonBJ 1 Reply Last reply
                  0
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #10

                    higher level:

                    curl
                    wget

                    1 Reply Last reply
                    1
                    • L Linz

                      Sorry for bothering you: I read the documentation for QXmlStreamReader, but I still have no clue how to filter the data. Maybe I am still missing the basics too much to understand what I need...

                      If I refer to the picture I want to save the first value 49.986, the second value 132.3 and the third value 0 in an array(s) of type double (to plot it later on).

                      But which function could do this for me?
                      I tried something like:
                      void QXmlStreamReader::addData(const QByteArray &buffer);

                      but it doesn't work. I could filter it easily if it would have been an array of char :/

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #11

                      @Linz
                      You just want to parse input XML. I don't think you'll particularly be needing QXmlStreamReader::addData(). You're just wanting to read elements and access values.

                      There is a Qt example at https://doc.qt.io/qt-5/qtxml-streambookmarks-example.html. But for you you're only interested in the XbelReader::readXBEL() part. It should get you started on how to read through an XML document.

                      1 Reply Last reply
                      1
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by VRonin
                        #12
                        const QByteArray replyData = reply->readAll();
                        QXmlStreamReader xmlreader(replyData);
                        bool rStarted = false;
                        while (!xmlreader.atEnd() && !xmlreader.hasError()) {
                            switch(xmlreader.readNext()){
                            case QXmlStreamReader::StartElement:
                                if(xmlreader.name()==QLatin1String("r"))
                                    rStarted = true;
                                else if(rStarted && xmlreader.name()==QLatin1String("f"))
                                    qDebug() << "f" << xmlreader.readElementText();
                                else if(rStarted && xmlreader.name()==QLatin1String("z"))
                                    qDebug() << "z" << xmlreader.readElementText();
                                else if(rStarted && xmlreader.name()==QLatin1String("p"))
                                    qDebug() << "p" << xmlreader.readElementText();
                                else if(rStarted && xmlreader.name()==QLatin1String("dt"))
                                    qDebug() << "dt" << xmlreader.readElementText();
                            break;
                            case QXmlStreamReader::EndElement:
                                if(xmlreader.name()==QLatin1String("r"))
                                    rStarted = false;
                            break;
                            default:
                            break;
                            }
                        }
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        L 1 Reply Last reply
                        5
                        • VRoninV VRonin
                          const QByteArray replyData = reply->readAll();
                          QXmlStreamReader xmlreader(replyData);
                          bool rStarted = false;
                          while (!xmlreader.atEnd() && !xmlreader.hasError()) {
                              switch(xmlreader.readNext()){
                              case QXmlStreamReader::StartElement:
                                  if(xmlreader.name()==QLatin1String("r"))
                                      rStarted = true;
                                  else if(rStarted && xmlreader.name()==QLatin1String("f"))
                                      qDebug() << "f" << xmlreader.readElementText();
                                  else if(rStarted && xmlreader.name()==QLatin1String("z"))
                                      qDebug() << "z" << xmlreader.readElementText();
                                  else if(rStarted && xmlreader.name()==QLatin1String("p"))
                                      qDebug() << "p" << xmlreader.readElementText();
                                  else if(rStarted && xmlreader.name()==QLatin1String("dt"))
                                      qDebug() << "dt" << xmlreader.readElementText();
                              break;
                              case QXmlStreamReader::EndElement:
                                  if(xmlreader.name()==QLatin1String("r"))
                                      rStarted = false;
                              break;
                              default:
                              break;
                              }
                          }
                          
                          L Offline
                          L Offline
                          Linz
                          wrote on last edited by
                          #13

                          @VRonin Thanks a lot! Where do you define readElementText() since I get the Error:
                          Error: use of undeclared identifier 'readElementText'

                          I tried to define it as QString readElementText() in the header, but it doesn't seem to work.

                          VRoninV 1 Reply Last reply
                          0
                          • L Linz

                            @VRonin Thanks a lot! Where do you define readElementText() since I get the Error:
                            Error: use of undeclared identifier 'readElementText'

                            I tried to define it as QString readElementText() in the header, but it doesn't seem to work.

                            VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #14

                            Sorry forgot to add xmlreader. amended now

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            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