[PyQt] QtWebKIt: traversing the domtree, how to get the tag-less items ?

Stef Mientki stef.mientki at gmail.com
Sat Jul 23 00:27:00 BST 2011


hello,

I'm having trouble finding tag-less elements in the dom tree.
Can someone explain what I'm doing wrong ?

# web2 is a QtWebView, and here it's filled with some contents
    frame = self.web2.page().mainFrame()
    frame.setHtml ( """<!DOCTYPE html>
<html>
  <head>
    <title>This is the Titls</title>
  </head>
  <body>
    <p>first line with p-tag</p>
    second line without any tag
    <p>second line with p-tag</p>
  </body>
</html>""" )


# I made a little function, to traverse the dom tree
def pr_dom_tree ( node, prefix ) :
  if node.isNull () :
    return
  print prefix, 'tag =', node.localName (), '// text =', node.toPlainText()
  FirstChild = node.firstChild()
  if not ( FirstChild.isNull() ) :
    pr_dom_tree ( FirstChild, prefix + '==' )
  node = node.nextSibling ()
  pr_dom_tree ( node, prefix )


# and using the dom tree parser here
print '========================='
doc = frame.documentElement()
pr_dom_tree ( doc, '' )

# results in:
=========================
 tag = html // text = first line with p-tag

second line without any tag
second line with p-tag
== tag = head // text =
    This is the Titls

==== tag = title // text = This is the Titls
== tag = body // text = first line with p-tag

second line without any tag
second line with p-tag
==== tag = p // text = first line with p-tag
==== tag = p // text = second line with p-tag

Looking at the end of the result, only 2 p-tag elements are visible.
What am I doing wrong ?

thanks,
Stef




More information about the PyQt mailing list