6

I want to make a function in PyQt evaluateJavaScript() (or may be similar one) and than display a result of evaluated function. Real function will be much bigger, and it might not be a string.

I'm only interesting in how to create a function inside PyQt code and than get the result into python variable.

To be more clear I will give you an example: that's the js that I want to type in after loadFinished on http://jquery.com:

w = document.getElementsByTagName('p')[0];
w.innerHTML

If I do it in browser console, I' will get an output:

"jQuery is a fast and concise JavaScript Library ...... blah blah blah"

And I want to store this output in a variable.

#!/usr/bin/env python

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import os, sys, signal
from urllib2 import urlopen

class GBot(QWebView):

    def __init__(self):
        QWebView.__init__(self)
        self.setPage(BrowserSettings())
        #self.jquery = get_jquery()
        self.load(QUrl('http://jquery.com'))
        self.frame = self.page().currentFrame()

    def _loadFinished(self, ok):
        doc = self.frame.documentElement()
        #doc.evaluateJavaScript(self.jquery)
        r = doc.evaluateJavaScript('''w = document.getElementsByTagName('p')[0]; w.innerHTML''')
        print r #want to do something like this


if __name__ == '__main__':
    app = QApplication(sys.argv)
    bot = GBot()
    bot.show()
    if signal.signal(signal.SIGINT, signal.SIG_DFL):
        sys.exit(app.exec_())
    app.exec_()
Vor
  • 33,215
  • 43
  • 135
  • 193

1 Answers1

7

In this example first I create a myWindow javascript object by passing self to the main frame, then call evaluateJavaScript when loadFinished:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtCore, QtGui, QtWebKit  

getJsValue = """ 
w = document.getElementsByTagName('p')[0];
myWindow.showMessage(w.innerHTML);
"""  

class myWindow(QtWebKit.QWebView):  
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)

        self.page().mainFrame().addToJavaScriptWindowObject("myWindow", self)

        self.loadFinished.connect(self.on_loadFinished)

        self.load(QtCore.QUrl('http://jquery.com'))

    @QtCore.pyqtSlot(str)  
    def showMessage(self, message):
        print "Message from website:", message

    @QtCore.pyqtSlot()
    def on_loadFinished(self):
        self.page().mainFrame().evaluateJavaScript(getJsValue) 

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myWindow')

    main = myWindow()
    main.show()

    sys.exit(app.exec_())
  • This really is an awesome answer because it's concise yet illustrates a lot of useful stuff. An English description would be helpful, though, for those who aren't yet fully fluent in Python, PyQt, and JavaScript. I'll give it a shot. – Jon Coombs Jan 01 '15 at 23:59
  • Can use any of this if useful: Although a myWindow object is a Python object, it inherits from QWebView and thus as it constructs itself can insert itself (using Qt) into the webpage as a JavaScript object. It also passes in two callbacks: 1) the on_loadFinished() method so it continue processing after the page has loaded; 2) a showMessage() method that the webpage's own JavaScript code can call. Once the page has loaded that triggers on_loadFinished(), which gives the webpage some JavaScript (getJsValue) to run, and that code in turn calls the Python method showMessage(). Full circle. – Jon Coombs Jan 02 '15 at 00:18