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_()
3
  • by passing it as a get or post parameter Commented Jan 8, 2013 at 4:24
  • can you give me an example? Commented Jan 8, 2013 at 4:24
  • 1
    Proper JQuery: w = $('p').children().eq(0).html(); Commented Jan 8, 2013 at 4:27

1 Answer 1

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_())
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.