我正在尝试使用python从HTML网站中提取一些javascript变量:
<script>
var nData = new Array();
var Data = "5b7b......";
nData = CallInit(Data);
...
...
</script>
我可以在萤火虫(DOM面板)中看到“ nData”的内容,而不会出现问题:
[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?这是可能的?
谢谢
借助python库Ghost.py,应该可以从执行的Javascript代码中获取动态变量。
我只是在一些小型测试站点上进行了尝试,并得到了一个名为Javascript的变量a
,该变量在该页面上用作python对象。我做了以下事情:
使用安装Ghost.py pip install Ghost.py
。
使用安装PySide(这是Ghost.py的先决条件)pip install PySide
。
使用以下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
放入python变量中。js_variable
ghost.open
ghost.evaluate('nData')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句