Bulk loading web pages using Qt

I want to write a program using Qt that downloads a large number of HTML web pages, about 5,000, from one site every day. After loading these pages, I need to extract some data using the DOM Query using the WebKit module, and then save this data to the database.

What is the best / correct / efficient way to do this, in particular the loading and analysis phase? How to handle the number of requests and how to create a "download manager"?

+3
source share
2 answers

To load pages, it makes sense to use a special library, for example libcurl

+2

, , , , QT.

( ) QT ( , QNetworkManager, QNetworkRequests, QNetworkReply). , , , . (, , / , )

, , , /html , .

Url, url-html, .

QQueue <QString> urlList .

    QQueue <String> workQueue; //First create somewhere a 
    int maxWorkers = 10;


    //Then create the workers
    void downloadNewArrivals::createWorkers(QString url){
checkNewArrivalWorker* worker = new checkNewArrivalWorker(url);
workQueue.enqueue(worker);
}

    //Make a function to control the amount of workers, 
    //and process the workers after they are finished

    void downloadNewArrivals::processWorkQueue(){
if (workQueue.isEmpty() && currentWorkers== 0){
    qDebug() << "Work Queue Empty" << endl;
} else if (!workQueue.isEmpty()){
    //Create the maxWorkers and start them in seperate threads
    for (int i = 0; i < currentWorkers && !workQueue.isEmpty(); i++){
        QThread* thread = new QThread;
        checkNewArrivalWorker* worker = workQueue.dequeue();
        worker->moveToThread(thread);
        connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
        connect(thread, SIGNAL(started()), worker, SLOT(process()));
        connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
        connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
        connect(thread, SIGNAL(finished()), this, SLOT(reduceThreadCounterAndProcessNext()));
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        thread->start();
        currentWorkers++;
    }
}
}

    //When finished, process the next worker
    void downloadNewArrivals::reduceThreadCounterAndProcessNext(){
currentWorkers--;  //This variable is to control amount of max workers

processWorkQueue();
    }


    //Now the worker
    //The worker class important parts..
    void checkNewArrivalWorker::getPages(QString url){
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest getPageRequest = QNetworkRequest(url); //created on heap 
getPageRequest.setRawHeader( "User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); "
                           "en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1" );
getPageRequest.setRawHeader( "charset", "utf-8" );
getPageRequest.setRawHeader( "Connection", "keep-alive" );
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyGetPagesFinished(QNetworkReply*)));
connect(manager, SIGNAL(finished(QNetworkReply*)), manager, SLOT(deleteLater()));
manager->get(getPageRequest);
}

    void checkNewArrivalWorker::replyGetPagesFinished(QNetworkReply *reply){
QString data = reply->readAll(); //Here data will hold your html to process as needed...
reply->deleteLater();
emit finished();


}

QString, , , DOM, .

, , .

0

Source: https://habr.com/ru/post/1769047/


All Articles