C++ 使用Qt从网站提取信息?
我想提取C++ 使用Qt从网站提取信息?,c++,html,qt,html-content-extraction,C++,Html,Qt,Html Content Extraction,我想提取btag=>123456789 这是HTML源代码: <body> <div> <table> <tbody> <tr> <td class="myclass"> <b>123456789&
b
tag=>123456789
这是HTML源代码:
<body>
<div>
<table>
<tbody>
<tr>
<td class="myclass">
<b>123456789</b>
</td>
</tr>
</tbody>
</table>
</div>
</body>
我有一个带有按钮(callupdate
)和LineEdit
)的表单,因此如果我单击update
按钮,LineEdit
应该自动设置文本123456789
。但是我的代码不起作用。行编辑的文本保持为空
我包括:
#include <QtWebKit>
#include <QtWebKitWidgets/QWebFrame>
#include <QWebView>
如前所述,您需要确保等待QWebView
中的数据加载足够长的时间
你可以这样做(非常简单):
将webView定义为dialog类的一部分,并声明一个稍后可以连接到web view信号的插槽
class My_Test_Dialog
{
public slots:
// slot to read your data once you are finished
void readPage(bool ok);
// whatever else you did
private:
QWebView *webView;
}
然后,例如,在构造函数中或其他地方,您可以创建webView,并将其loadFinished()
信号连接到上面的类定义中所示的readPage()
插槽
// create QWebview and connect its loadFinished signal to our slot
webView = new QWebView(this);
QObject::connect(webView,SIGNAL(loadFinished(bool)), this, SLOT( readPage(bool) ) );
然后,在您的on_button_clicked()
方法中,您只加载页面(如果需要,则显示webview)
然后,一旦对话框完成加载,将自动调用插槽readData()
,您只需在那里执行读取操作即可
void MyDialog::readPage(bool ok)
{
// get HTML element information
QWebElementCollection colls = webView->page()->mainFrame()->findAllElements("td.myclass b");
foreach(QWebElement elemento, colls)
{
lineEdit->setText(elemento.toInnerXml());
}
}
让我知道这是否有帮助 如前所述,您需要确保等待足够长的时间才能加载QWebView
中的数据
你可以这样做(非常简单):
将webView定义为dialog类的一部分,并声明一个稍后可以连接到web view信号的插槽
class My_Test_Dialog
{
public slots:
// slot to read your data once you are finished
void readPage(bool ok);
// whatever else you did
private:
QWebView *webView;
}
然后,例如,在构造函数中或其他地方,您可以创建webView,并将其loadFinished()
信号连接到上面的类定义中所示的readPage()
插槽
// create QWebview and connect its loadFinished signal to our slot
webView = new QWebView(this);
QObject::connect(webView,SIGNAL(loadFinished(bool)), this, SLOT( readPage(bool) ) );
然后,在您的on_button_clicked()
方法中,您只加载页面(如果需要,则显示webview)
然后,一旦对话框完成加载,将自动调用插槽readData()
,您只需在那里执行读取操作即可
void MyDialog::readPage(bool ok)
{
// get HTML element information
QWebElementCollection colls = webView->page()->mainFrame()->findAllElements("td.myclass b");
foreach(QWebElement elemento, colls)
{
lineEdit->setText(elemento.toInnerXml());
}
}
让我知道这是否有帮助 在单击“更新”按钮之前,请考虑给webview一点时间来加载页面,直到页面完成。至少你的代码是这么说的。您将网站加载到点击按钮的插槽中。在同一时段中,您尝试评估结果。即时下载?使用QWebView的loadFinished信号确定url加载何时完成。然后您可以尝试评估内容。我如何使用loadFinished()?你能举个例子吗?在你点击“更新”按钮之前,考虑给webview一段时间来加载页面完成。不,你没有。至少你的代码是这么说的。您将网站加载到点击按钮的插槽中。在同一时段中,您尝试评估结果。即时下载?使用QWebView的loadFinished信号确定url加载何时完成。然后您可以尝试评估内容。我如何使用loadFinished()?你能举个例子吗