![](/img/trans.png)
[英]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.