使用QWebkit检索具有特定类的div [英] Using QWebkit to retrieve divs with a specific class

查看:165
本文介绍了使用QWebkit检索具有特定类的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面发布了问题,试图使用QDomDocument类.建议我改用QWebkit,但我对如何使用QWebkit进行操作感到非常困惑.我以前从未使用过它,所以我不太确定.有人可以提供任何建议吗?谢谢! 作为记录,该函数使用QByteArray,将其转换为文本后即为标准HTML文件.

I posted the question below, trying to use the QDomDocument classes. I was advised to use the QWebkit instead, but I'm very confused how to do what I need to do with QWebkit. I've never used it before so I'm rather unsure with it. Could anyone please offer any advice? Thanks! For the record, the function is using a QByteArray that when translated to text is a standard HTML file.

原始问题:

在HTML文件中,我有几个具有不同类的div,如下所示:

I have several divs in an HTML file with different classes, like this:

<div class='A'>...</div>
<div class='B'>...</div>
<div class='C'>...</div>

我有一个Qt(4.7)程序,在该程序中我需要能够基于该类从中获得一定的div.我需要在此程序中使用QDomDocument.我从文档中知道该类具有功能elementById(),但是我不能让它与类一起使用,而只能使用id.这不是由HTML文件制成的,也不是任何东西,因此我无法控制它是类还是id.有没有办法做到这一点,我很想念?谢谢!

I have a Qt (4.7) program where I need to be able to get a certain div out of this based on the class. I need to use QDomDocument in this program. I know from the documentation that that class has a function elementById(), but I can't get that to work with classes, just ids. This isn't a HTML file a made or anything, so I don't have any control over whether it's class or id. Is there a way to do this that I'm missing? Thanks!

推荐答案

.pro

QT += webkitwidgets

main.cpp

#include <QApplication>
#include <QDebug>
#include <QWebView>
#include <QWebFrame>
#include <QWebElement>

int main( int argc, char *argv[] ) {
    QApplication a(argc, argv);

    QString l_html( "<html><body>"
                    "<div class='A'>div with class A</div>"
                    "<div class='B'>div with class B</div>"
                    "<div class='C'>div with class C</div>"
                    "<span class='A'>span with class A</span>"
                    "</body></html>" );

    QWebView l_webView; // you can skip the QWebView if you dont want to show any widget
    l_webView.page()->mainFrame()->setHtml( l_html );
    QWebElement l_root( l_webView.page()->mainFrame()->documentElement() );
    QWebElementCollection l_elements( l_root.findAll( ".a" ) );

    foreach ( QWebElement l_e, l_elements ) {
        // do what you want here
    }

    return a.exec();
}

这篇关于使用QWebkit检索具有特定类的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆