派赛德.JavaScript.执行 js 获取结果为纯文本或 html [英] PySide. JavaScript. Execute js an get result as a pure text or html

查看:104
本文介绍了派赛德.JavaScript.执行 js 获取结果为纯文本或 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想从 http://www.dukascopy.com/swiss/english/marketwatch/calendars/eccalendar/数据是动态的并使用 js-applet 加载 - DukascopyApplet"我试图以这种方式提取数据:

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

但它显示无".我用的是win7、Py3.3、pyside.

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

那么问题是如何获取加载页面上存在的js代码和/或applet的结果?

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

推荐答案

Deciding by PySide 文档 QWebFrame.evaluateJavaScript,它...

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

使用此框架作为上下文评估由 scriptSource 定义的 JavaScript,并返回最后执行语句的结果.

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

Se 肯定应该从 JS 返回一些东西.

Se definitely it should be returning something from the JS.

因此,了解为什么从该方法调用中获得 None 的唯一方法是了解 JS 端最后执行的语句"的实际值是多少.(作为旁注,JS 端可能实际上返回一个 nullundefined 值,这将正确映射到 PythonNone 并打印为None".)

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

我建议您首先尝试执行一个非常简单的 Javascript 片段,它返回一个您已知的常量硬编码值,然后看看是否可行.如果问题仍然存在,我会首先建议在那个非常非常简单的代码段上解决它,并且只有在它起作用后,才能转到您真正的 Javascript 代码.

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.

这篇关于派赛德.JavaScript.执行 js 获取结果为纯文本或 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆