下面是我用来获取给定节点的所有子节点列表的代码片段。但是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()
答案 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