Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I get data from Wikipedia?



  • I have a class for parsing pages:
    loader.h

    #ifndef LOADER_H
    #define LOADER_H
    
    #include <QObject>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    
    class Loader : public QObject
    {
        Q_OBJECT
    public:
        explicit Loader(QObject *parent = nullptr);
        void makeRequest(QString endPointRequest);
    signals:
        void dataReadyRead(QByteArray);
    public slots:
        void ReadRead(QNetworkReply *reply);
    private:
        QNetworkAccessManager *m_manager= new QNetworkAccessManager(this);
    };
    
    #endif // LOADER_H
    

    loader.cpp

    #include "loader.h"
    
    Loader::Loader(QObject *parent) : QObject(parent)
    {
        connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ReadRead(QNetworkReply*)));
    }
    
    void Loader::makeRequest(QString endPointRequest)
    {
        m_manager->get(QNetworkRequest(QUrl(endPointRequest)));
    }
    
    void Loader::ReadRead(QNetworkReply *reply)
    {
        QByteArray myData;
        myData = reply->readAll();
        emit dataReadyRead(myData);
    }
    

    And in the basic form I pass the reference for parsing :

    MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            connect(&theLoader,SIGNAL(dataReadyRead(QByteArray)),this,SLOT(dataUploaded(QByteArray)) );
        }
        ...
        void MainWindow::on_pushButton_4_clicked()
        {
            theLoader.makeRequest("http://ru.wikipedia.org/w/api.php?action=query&format=json&titles=Ключевая%20ставка%20в%20России");
        }
        
        void MainWindow::dataUploaded(QByteArray data)
        {
            QString dataString;
            dataString = data;
            ui->apiResponseDisplay->setText(dataString);
            qDebug()<<dataString;
        }
    

    This code returns a blank page: "".
    How can I get data from Wikipedia?



  • Looks like you are just not handling unicode correcly. could you try with an ascii only process and see if that works, you can use https://en.wikipedia.org/w/api.php?action=query&format=json&titles=lemon

    Also, make sure you have OpenSSL correctly set up as this is an https request.

    You can connect to http://doc.qt.io/qt-5/qnetworkreply.html#sslErrors and http://doc.qt.io/qt-5/qnetworkreply.html#error-1 to detect what's going wrong



  • @VRonin I always get empty results from Wikipedia. Can you write an example, output errors? I do not understand how to use the methods you referred to.



  • @VRonin if(reply->error()) {
    qDebug() << "Error!";
    qDebug() << reply->errorString();
    }
    Does not return an error.



  • Try this minimal example:

    #include <QCoreApplication>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QNetworkRequest>
    #include <QDebug>
    #include <QTextStream>
    #include <QSslError>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication application(argc, argv);
        QNetworkAccessManager qnam;
        const QNetworkRequest wikireq(QUrl(QStringLiteral("https://en.wikipedia.org/w/api.php?action=query&format=json&titles=lemon")));
        QNetworkReply* wikirep = qnam.get(wikireq);
        QObject::connect(wikirep,&QNetworkReply::finished,[wikirep]()->void{
            QTextStream repStream(wikirep);
            qDebug() << "Response Received:\n" << repStream.readAll();
            wikirep->deleteLater();
        });
        QObject::connect(wikirep,QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),[wikirep]()->void{
            qDebug() << "Network Error:\n" << wikirep->errorString();
            wikirep->deleteLater();
        });
        QObject::connect(wikirep,&QNetworkReply::sslErrors,[wikirep](const QList<QSslError> &errors)->void{
            qDebug() << "SSL Errors:";
            for(auto&& err : errors)
                qDebug() << err.errorString();
            wikirep->deleteLater();
        });
        return application.exec();
    }
    


  • @VRonin nothing returns""



  • @VRonin even no error



  • @VRonin behold:
    qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
    qt.network.ssl: QSslSocket: cannot call unresolved function SSL_CTX_new
    qt.network.ssl: QSslSocket: cannot call unresolved function SSL_library_init
    qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error
    qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error
    Network Error:
    "Error creating SSL context ()"
    Response Received:
    ""





  • @Taz742 On Windows / Thanks. I decided already this problemma.link helped


Log in to reply