python QtWebkit:nextSibing不返回null或None

时间:2011-04-23 19:44:27

标签: python qt pyqt qtwebkit

下面是我用来获取给定节点的所有子节点列表的代码片段。但是nextSibling()永远不会返回null,所以while循环永远执行。请帮忙。

 children = [ ]
 children.append(documentElement.firstChild())
 curr_node = children[0]
 while curr_node.nextSibling():
     print curr_node, len(children)
     children.append(curr_node.nextSibling())
     curr_node = curr_node.nextSibling()

1 个答案:

答案 0 :(得分:2)

据我所知,nextSibling将始终返回一个QWebElement,但您可以使用isNull()

检查是否为null元素
while not curr_node.nextSibling().isNull():
     print curr_node, len(children)
     children.append(curr_node.nextSibling())
     curr_node = curr_node.nextSibling()

您可以在http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwebelement.html#isNull中查看 http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwebelement.html#nextSibling