gpt4 book ai didi

c++ - 使用 QWebView 和 QWebElement 的网页抓取返回增加的倍数

转载 作者:行者123 更新时间:2023-11-28 02:22:56 25 4
gpt4 key购买 nike

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

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

关于c++ - 使用 QWebView 和 QWebElement 的网页抓取返回增加的倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com