[PyKDE] How do you get HTML source from konqueror/KHTMLPart?

yichun wei yichun.wei at gmail.com
Thu Dec 21 22:41:24 GMT 2006


could I get a piece of code where .toString () works without throwing
a DOM::DOMException instance and not crashing the application?

Thanks a lot! The code I have, which crashes will be attached in the following:
-----------------------------------------
from kdecore import KCmdLineArgs, KURL, KApplication, i18n,
KAboutData, BarIcon, KLibLoader
from kdeui import KMainWindow, KMessageBox, KAction, KStdAction,
KKeyDialog, KEditToolbar
from qt import  QString, QStringList
from kio import KTrader, KIO
from khtml import KHTMLPart, KHTMLView
# Importing the KParts namespace gets us all of the KParts:: classes
from kparts import KParts, createReadOnlyPart, createReadWritePart

import sys, os

FALSE = 0
TRUE  = not FALSE

TOOLBAR_EXIT = 0
TOOLBAR_OPEN = 1

class pyPartsMW (KParts.MainWindow):
        def __init__ (self, *args):
                apply (KParts.MainWindow.__init__, (self,) + args)

                quitAction    = KStdAction.quit (self.close,
self.actionCollection ())

                self.m_toolbarAction =
KStdAction.showToolbar(self.optionsShowToolbar,
self.actionCollection());
                self.m_statusbarAction =
KStdAction.showStatusbar(self.optionsShowStatusbar,
self.actionCollection());

                KStdAction.keyBindings(self.optionsConfigureKeys,
self.actionCollection());

KStdAction.configureToolbars(self.optionsConfigureToolbars,
self.actionCollection());

                self.path = os.getcwd () + '/'
                self.setGeometry (0, 0, 600, 500)

                self.w   = KHTMLPart (self);
                self.w.openURL (KURL ("http://www.google.com"));
                self.w.view ().setGeometry (0, 0, 600, 500);

                domDoc = self.w.document()
                html = domDoc.toString().string()
                print html


        def optionsShowToolbar (self):
                if self.m_toolbarAction.isChecked():
                        self.toolBar().show()
                else:
                        self.toolBar().hide()

        def optionsShowStatusbar (self):
                if self.m_statusbarAction.isChecked ():
                        self.statusBar().show()
                else:
                        self.statusBar().hide()
        def optionsConfigureKeys (self):
                KKeyDialog.configureActionKeys
(self.actionCollection(), self.xmlFile ())


        def optionsConfigureToolbars (self):
                dlg = KEditToolbar (self.actionCollection(), self.xmlFile ())
                if dlg.exec_loop ():
                        self.createGUI(self);


        def queryClose(self):
                res = KMessageBox.warningYesNoCancel(self,\
                        i18n("Save changes to Document?<br>(Does not
make sense, we know, but it is just a programming example :-)"))
                if res == KMessageBox.Yes:
                        return TRUE

                elif res == KMessageBox.No:
                    return TRUE

                else: #// cancel
                        return FALSE

        def queryExit(self):
                return TRUE #// accept

        # Session management: save data
        def saveProperties(self, config):
                config.writeEntry("text", self.edit.text())

        # Session management: read data again
        def readProperties(self, config):
                self.edit.setText(config.readEntry("text"))



#------------- main ----------------------------

description = "KHTMLPart - simple example"
version = "0.1"
aboutData = KAboutData ("pyKHTMLPart", "pyHTMLPart",\
    version, description, KAboutData.License_GPL,\
    "(c) 2002, Jim Bublitz")

KCmdLineArgs.init (sys.argv, aboutData)

app = KApplication ()
parts = pyPartsMW (None, "pyParts")
parts.show()
app.setMainWidget(parts)
app.exec_loop()
----------------------------------------------------------------------------------


$ python kwebreader.py
...
terminate called after throwing an instance of 'DOM::DOMException'
KCrash: Application 'kwebreader.py' crashing...
Could not find 'drkonqi' executable.
KCrash cannot reach kdeinit, launching directly.

This is modified from Jim's pyHTMLPart.py.  Could you give me any hint
that why .toString() crashes this script? thanks!


- yichun

On 12/21/06, Marcos Dione <mdione at grulic.org.ar> wrote:
> On Wed, Dec 20, 2006 at 05:40:03PM -0800, yichun wei wrote:
> > where .toString() is called. Is there any other reason leading to this
> > other than the 0 values when initiate the KHTMLPart object?
>
>     I'm not sure, and I didn't explore it too much, because I works for
> me (I'm using it to browse the web). maybe you can run it under gdb to
> work it out.
>
> --
> (Not so) Random fortune:
> Enjoy the party while it lasts.
>




More information about the PyQt mailing list