PySide. JavaScript. Execute js an get result as a pure text or html

百般思念 提交于 2019-12-14 03:14:22

问题


Would like to get calendar data from page like http://www.dukascopy.com/swiss/english/marketwatch/calendars/eccalendar/ The data is dynamic and loads with js-applet - "DukascopyApplet" I tried to extract data this way:

app = QApplication(sys.argv)
web = QWebView()
web.page().mainFrame().loadFinished.connect(print_content)
web.load(QUrl("http://www.dukascopy.com/swiss/english/marketwatch/calendars/eccalendar/"))
web.show()

frame = web.page().mainFrame() 

print(frame.evaluateJavaScript("DukascopyApplet"))

sys.exit(app.exec_())

But it shows "none". I am using win7, Py3.3, pyside.

So the question is how to get the result of js code and/or applet, which exists on the loaded page?


回答1:


Deciding by PySide documentation for QWebFrame.evaluateJavaScript, it...

Evaluates the JavaScript defined by scriptSource using this frame as context and returns the result of the last executed statement.

Se definitely it should be returning something from the JS.

Therefore, the only way to get an idea why you're getting None out of that method invocation, is to know what is the actual value of the "last executed statement" on the JS side. (As a side note, it's possible that the JS side is actually returning a null or undefined value, which would be correctly mapped to Python as None and printed as "None".)

I propose you first try to execute a very very simple snippet of Javascript that returns a constant hardcoded value known to you, and see if that works out. If the problem persists, I would first suggest solving it on that very very simple snippet, and only once that's working, moving on to your real Javascript code.

The lesson of the story is that debugging should start by reducing the problem incrementally so that at some point the problem disappears, and then working out the exact moment that it appears again. Otherwise you're just fighting something you haven't even found/seen.




回答2:


The QWebFrame.evaluateJavaScript will do it, but it doesn't work that well. It doesn't always return the right type, and I've found that it always returns None on function calls. One way of getting the result is to set the returned item to a variable you have access to then call evaluateJavaScript again on that variable.

frame.evaluateJavaScript("myVariable = getResult()")
result = frame.evaluateJavaScript("myVariable")

I just looked at the "DukascopyApplet", and its not a typical data type. Only standard data types like strings, ints, float, bools ... can be transferred or returned to python. You will probably have to go through the "DukascopyApplet" and find the specific data you want. Another thing that may help is to attach a Python object to the JavaScript. You can then call that python object's slot methods inside of your JavaScript.

class MyCLass(object):
    @QtCore.Slot(str)
    def doSomething(self, info):
        # do something with the string info here

frame.addToJavaScriptWindowObject("varName", MyClass)
frame.evaluateJavaScript("varName.doSomething(DukascopyApplet.params.height)")

web.settings().setAttribute(QtWebKit.QWebSettings.WebAttribute.DeveloperExtrasEn‌​abled, True)
inspector = QtWebKit.QWebInspector()
inspector.setPage(web.page())

After looking through the DukascopyApplet it doesn't look like there is any useful stored information there. Finding that data may be difficult.



来源:https://stackoverflow.com/questions/20047325/pyside-javascript-execute-js-an-get-result-as-a-pure-text-or-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!