我试图用python从HTML网站中提取一些javascript变量:
<script>
var nData = new Array();
var Data = "5b7b......";
nData = CallInit(Data);
...
...
</script>
我可以看到&#34; nData&#34;的内容在firebug(DOM面板)中没有问题:
[Object { height="532", width="1280", url="https://example.org...8EDA4F3F5F395B9&key=lh1", more...}, Object { height="266", width="640", url="https://example.org...8EDA4F3F5F395B9&key=lh1", more...}]
nData的内容是一个URL。 如何解析/提取nData的内容到python? 它有可能吗?
由于
答案 0 :(得分:3)
在python库Ghost.py的帮助下,应该可以从执行的Javascript代码中获取动态变量。
我刚试了一些small test site并得到了一个名为a
的Javascript变量,我在该页面上使用它作为python对象。我做了以下事情:
使用pip install Ghost.py
安装Ghost.py。
使用pip install PySide
安装PySide(它是Ghost.py的先决条件)。
使用以下python代码:
from ghost import Ghost
ghost = Ghost()
ghost.open('https://dl.dropboxusercontent.com/u/13991899/test/index.html')
js_variable, _ = ghost.evaluate('a', expect_loading=True)
print js_variable
您应该可以通过nData
打开您的网站,将变量js_variable
放入python变量ghost.open
,然后拨打ghost.evaluate('nData')
。