Unsolved Read data of a website
-
Hello qt-forum,
what do I want to do?
- read in data from a website
- every 5 seconds
- plot it
Where do I struggle?
- read in the data since I am a beginner I have no idea how to realize it. It should be possible with QNetwork, but everything I tried didn't work :/
Hopefully you have an idea, would help me a lot :)
You can see how the website looks in the picture. !Thanks!
-
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. -
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?
-
@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 -
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.
-
Hi,
No need to convert anything.
You can access the internal data using the QByteArray::data function.
-
@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!
-
I somehow missed the XML part.
My fellows are correct, use QXmlStreamReader.
-
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 :/
-
higher level:
curl
wget -
@Linz
You just want to parse input XML. I don't think you'll particularly be needingQXmlStreamReader::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. -
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; } }
-
@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.
-
Sorry forgot to add
xmlreader.
amended now