从html中提取/解码Javascript变量到python

时间:2015-04-17 09:06:09

标签: javascript python html http xbmc

我试图用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? 它有可能吗?

由于

1 个答案:

答案 0 :(得分:3)

在python库Ghost.py的帮助下,应该可以从执行的Javascript代码中获取动态变量。

我刚试了一些small test site并得到了一个名为a的Javascript变量,我在该页面上使用它作为python对象。我做了以下事情:

  1. 使用pip install Ghost.py安装Ghost.py。

  2. 使用pip install PySide安装PySide(它是Ghost.py的先决条件)。

  3. 使用以下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
    
  4. 您应该可以通过nData打开您的网站,将变量js_variable放入python变量ghost.open,然后拨打ghost.evaluate('nData')