简体   繁体   English

使用Qt从网站提取信息?

[英]extract information from a website using Qt?

I'd like extract the information in the b tag => 123456789 我想在b标签中提取information => 123456789

this is the HTML source : 这是HTML来源:

<body>
       <div>
          <table>
               <tbody>
                     <tr>
                         <td class="myclass">
                               <b>123456789</b>
                         </td>
                     </tr>
              </tbody>
          </table>
       </div>
 </body>

So, I tried this : 所以,我尝试了这个:

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());
        }
}

I have a form with a Button( call update ) and a LineEdit , so if I click on the update button, the LineEdit should set the text 123456789 automatically. 我有一个带有Button( call update )和LineEdit ,因此,如果单击update按钮,则LineEdit应该自动设置文本123456789 But my code doesn't work. 但是我的代码不起作用。 The Text of the LineEdit remains empty. LineEdit的文本保持为空。

I include this : 我包括:

#include <QtWebKit>
#include <QtWebKitWidgets/QWebFrame>
#include <QWebView>

QT file.pro is : QT file.pro是:

QT += core gui
QT += network
QT += webkit
QT += webkitwidgets

As already mentioned you need to make sure that you wait long enough for the data in your QWebView to load. 如前所述,您需要确保等待足够长的时间以加载QWebView的数据。

You can do this (very simplistic) with something like this: 您可以使用以下方法(非常简单)进行此操作:

Define the webView as part of dialog class and also declare a slot that you can later connect to the signal of the web view 将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;

}

Then eg in the constructor or elsewhere you can create the webView and connect its loadFinished() signal to the readPage() slot shown also in the class definition above 然后,例如,在构造函数中或其他地方,您可以创建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) ) );

then in your on_pushButton_clicked() method you only load the page (and show the webview if that is what you need) 然后在on_pushButton_clicked()方法中,您仅加载页面(并在需要时显示webview)

void My_Test_Dialog::on_pushButton_clicked()
{
  webView->load(QUrl("http://www.esesese.com"));
}

and then once the dialog finished loading the slot readData() will be automatically called and there you can simply do your read operation 然后对话框完成加载后,将自动调用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());
    }

}

Let me know if this helps. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM