开发者

how to install pythonwebkit and pywebkitgtk in ubuntu?

i am a new to use ubuntu. i want to install pythonwebkit and pywebkitgtk in ubuntu,but i have tryed a long time. even thongth i can install,but,when i excute this code,

#!/usr/bin/env python

import pygtk
pygtk.require("2.0")
import gi
from gi.repository import WebKit as webkit, Gtk as gtk

dir(gtk)
print dir(gtk.WindowType)

init_string="""



<div id="testing">testing</div>
<div><button id="link">CLICK ME</button>


"""

class Browser:
    # Ventana del programa
    def __init__(self):
        self.window = gtk.Window(type=gtk.WindowType.TOPLEVEL)
        self.window.set_position(gtk.WindowPosition.CENTER)
        self.window.set_default_size(800, 600)
        self.window.fullscreen()
        self.window.connect("destroy", self.on_quit)

        # Un vBox, en la parte de arriba hay una caja para ingresar
        # la direccion web, y abago se muestra la pagina
        vbox = gtk.VBox()

        # La parte en donde se muestra la pagina que se visita (con scroll incluido)
        self.scroll_window = gtk.ScrolledWindow()
        self.webview = webkit.WebView()
        print dir(self.webview)

        #self.scroll_window.add(self.webview)

        # Unimos todo e开发者_如何学编程n el vBox
        #vbox.pack_start(self.url_text, True, False, 0)
        # El expand=False al empaquetarlo es para que el entry no ocupe media pantalla
        #vbox.pack_start(self.scroll_window, True, True, 0)
        #self.window.add(vbox)
        self.window.add(self.webview)
        self.window.show_all()
        self.webview.load_string(init_string, "text/html", "utf-8", '#')
        doc = self.webview.get_dom_document()
        self.webview.connect('document-load-finished', self.onLoad)
        print doc
        print dir(doc)

    def onLoad(self, param1, param2):
        print "STARTING"
        doc = self.webview.get_dom_document()
        div = doc.get_element_by_id('testing')
        print div
        print dir(div)
        print div.get_inner_html()
        div.set_inner_html('DYNAMIC TEXT')

        link = doc.get_element_by_id('link')
        #print link
        #print dir(link)
        link.connect('click-event', self.onClick, link)
        #div.connect_object('click-event', self.onClick, link)

    def print_childs(self, elem):
        childs = elem.get_child_nodes()
        for c in range(childs.get_length()):
            i=childs.item(c)
            #print dir(i)
            print "%s - %s\n" %(i.get_tag_name(), i.get_inner_html())
            self.print_childs(i)

    def onClick(self, p1, p2, p3=None):
        print "CLICKED - %s %s %s " % (str(p1), str(p2), str(p3))
        #return False

    def on_quit(self, widget):
        gtk.main_quit()

if __name__ == "__main__":
    browser = Browser()
    try:
      while True:
          gtk.main_iteration()
    except:
      gtk.quit()
      print "BAILING OUT!"

the "doc = self.webview.get_dom_document()" can't pass,the error is webview does not have the get_dom_document attr. what should i can do? i must access the dom tree, please! i know the way i install pythonwebkit or pywebkitgtk has something wrong,but i can't do right. is someone can help?


Run your import directives in the interpreter.

If that doesn't throw any exceptions (as I gather from where you say the error arises), then the problem is most likely not with installing the libraries, and has nothing to do with Ubuntu.

Python is not saying 'hey, this is not installed correctly', it's saying 'hey, you're handling this object incorrectly! The function you want to reach is not there!'.

Recheck the approapiate documentation, and see how to get the DOM tree.

edit -- I got this from a quick google query.

def _view_load_finished_cb(self, view, frame):
        doc = frame.get_dom_document()
        nodes = doc.getElementsByTagName('body')
        body = nodes.item(0)

You're trying to access the DOM tree from an object that doesn't provide it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜