使用 QWebView 和 QWebElement returns 增加倍数进行网页抓取

Web scraping with QWebView and QWebElement returns increasing multiples

我目前正在开发一款可以查询 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();
}

我的结果是这样的:

Ankh of Mishra
Basalt Monolith
Basalt Monolith
Black Lotus
Black Lotus
Black Lotus
Black Vise
Black Vise
Black Vise
Black Vise

在我添加事件循环之前,我只会得到 i 次卡片名称,现在它似乎是根据循环中的数字来匹配的。

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

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

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