C++ 使用QWebView和QWebElement进行Web抓取会返回越来越多的倍数

C++ 使用QWebView和QWebElement进行Web抓取会返回越来越多的倍数,c++,qt,web-scraping,qwebview,qwebelement,C++,Qt,Web Scraping,Qwebview,Qwebelement,我目前正在开发一个软件,该软件将查询gatherer.magic.com,以建立一个卡片数据库。在测试我的功能时,我发现我得到了奇怪的结果。我的职能如下: void cardDB::updateDB() { this->view = new QWebView; QString urlString("http://gatherer.wizards.com/Pages/Card/Details.aspx? multiverseid="); for(int i = 1

我目前正在开发一个软件,该软件将查询gatherer.magic.com,以建立一个卡片数据库。在测试我的功能时,我发现我得到了奇怪的结果。我的职能如下:

void cardDB::updateDB()
{
    this->view = new QWebView;
    QString urlString("http://gatherer.wizards.com/Pages/Card/Details.aspx?  multiverseid=");

    for(int i = 1; i <= 4; i++)
    {

        // Load the page
        view->load(QUrl(urlString+QString::number(i)));
        QObject::connect(view, SIGNAL(loadFinished(bool)), this, SLOT(saveFile()));

        // Wait for saveFile() to finish
        QEventLoop loop;
        QObject::connect(this, SIGNAL(done()), &loop, SLOT(quit()));

        loop.exec();
    }
}

void cardDB::saveFile()
{
    QString fileName("test");
    // Grab the name tag
    QWebElement e = view->page()->mainFrame()->findFirstElement("div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_nameRow");
    QString pageString = e.toPlainText();
    pageString.remove(0, 11);

    QFile localFile(fileName +".txt");
    if (!localFile.open(QIODevice::Append))
    {
        // Still need to implement error catching
    }
    else
    {
        localFile.write(pageString.toUtf8());
        localFile.close();
    }

    emit done();
}

在添加事件循环之前,我只需获得I卡名称I times,现在它似乎根据循环中的数字匹配。

在for循环末尾添加的以下代码行修复了我的问题:

QObject::disconnect(view, SIGNAL(loadFinished(bool)), this, SLOT(saveFile()));

我相信这是因为在循环的每次迭代中,我都会连接一个新的信号/插槽组合,这样每次都会在loadFinished信号通过时发生。

我以前就被这个信号捕获过——我一直认为,如果在连接之前进行测试,看看是否已经建立了连接,那不是很好。可能有,但我还没有找到。在与我的一位非常了解Qt的朋友交谈时,他还指出我可以使用:
QObject::connect(视图,信号(loadFinished(bool)),this,SLOT(saveFile()),Qt::UniqueConnection)这就是你问的关于Michael的问题
QObject::disconnect(view, SIGNAL(loadFinished(bool)), this, SLOT(saveFile()));