最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to put result of JavaScript function into python variable. PyQt - Stack Overflow

programmeradmin2浏览0评论

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 :

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(''))
        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_()

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

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.'))
        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_()
Share Improve this question asked Jan 8, 2013 at 4:22 VorVor 35.2k46 gold badges141 silver badges196 bronze badges 3
  • by passing it as a get or post parameter – Ibu Commented Jan 8, 2013 at 4:24
  • can you give me an example? – Vor Commented Jan 8, 2013 at 4:24
  • 1 Proper JQuery: w = $('p').children().eq(0).html(); – Mr. Polywhirl Commented Jan 8, 2013 at 4:27
Add a ment  | 

1 Answer 1

Reset to default 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.'))

    @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_())
发布评论

评论列表(0)

  1. 暂无评论