Load a Web source code into a String or QString to parse
-
Hello, (please any help will be wellcomed)
I can display a web with the next commandsQWebView *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
-
Hello, (please any help will be wellcomed)
I can display a web with the next commandsQWebView *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
@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 }
-
Hello, (please any help will be wellcomed)
I can display a web with the next commandsQWebView *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
-
@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 }
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-pageclass 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; }
-
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-pageclass 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; }
-
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.
@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.