[英]Extract an information from the source code of an internet page with Qt Creator
[英]extract information from a website using Qt?
我想在b
标签中提取information
=> 123456789
这是HTML来源:
<body>
<div>
<table>
<tbody>
<tr>
<td class="myclass">
<b>123456789</b>
</td>
</tr>
</tbody>
</table>
</div>
</body>
所以,我尝试了这个:
void My_Test_Dialog::on_pushButton_clicked()
{
QWebView *webview = new QWebView(parentWidget());
webview->load(QUrl("http://www.esesese.com"));
webview->show();
// get HTML element information
QWebElementCollection colls = webview->page()->mainFrame()->findAllElements("td.myclass b");
foreach(QWebElement elemento, colls)
{
ui->lineEdit_data->setText(elemento.toInnerXml());
}
}
我有一个带有Button( call update
)和LineEdit
,因此,如果单击update
按钮,则LineEdit
应该自动设置文本123456789
。 但是我的代码不起作用。 LineEdit
的文本保持为空。
我包括:
#include <QtWebKit>
#include <QtWebKitWidgets/QWebFrame>
#include <QWebView>
QT file.pro是:
QT += core gui
QT += network
QT += webkit
QT += webkitwidgets
如前所述,您需要确保等待足够长的时间以加载QWebView
的数据。
您可以使用以下方法(非常简单)进行此操作:
将webView定义为对话框类的一部分,并声明一个插槽,以后可以将其连接到Web视图的信号
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_pushButton_clicked()
方法中,您仅加载页面(并在需要时显示webview)
void My_Test_Dialog::on_pushButton_clicked()
{
webView->load(QUrl("http://www.esesese.com"));
}
然后对话框完成加载后,将自动调用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());
}
}
让我知道是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.