如何在Python中评估JavaScript代码
我需要从一个网页上获取一些结果,这个网页使用了一些JavaScript代码来生成我感兴趣的部分,像下面这样:
eval(function(p,a,c,k,e,d){e=function(c){return c};if(!''.replace(/^/,String)){while(c--)d[c]=k[c]||c;k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('5 11=17;5 12=["/3/2/1/0/13.4","/3/2/1/0/15.4","/3/2/1/0/14.4","/3/2/1/0/7.4","/3/2/1/0/6.4","/3/2/1/0/8.4","/3/2/1/0/10.4","/3/2/1/0/9.4","/3/2/1/0/23.4","/3/2/1/0/22.4","/3/2/1/0/24.4","/3/2/1/0/26.4","/3/2/1/0/25.4","/3/2/1/0/18.4","/3/2/1/0/16.4","/3/2/1/0/19.4","/3/2/1/0/21.4"];5 20=0;',10,27,'40769|54|Images|Files|png|var|imanhua_005_140430179|imanhua_004_140430179|imanhua_006_140430226|imanhua_008_140430242|imanhua_007_140430226|len|pic|imanhua_001_140429664|imanhua_003_140430117|imanhua_002_140430070|imanhua_015_140430414||imanhua_014_140430382|imanhua_016_140430414|sid|imanhua_017_140430429|imanhua_010_140430289|imanhua_009_140430242|imanhua_011_140430367|imanhua_013_140430382|imanhua_012_140430367'.split('|'),0,{}))
我觉得eval()
的结果对我很重要,我正在写一个Python脚本,请问有没有什么库可以让我在虚拟环境中运行这段JavaScript代码并获取输出呢?
谢谢
6 个回答
4
你可以使用PyQt和WebKit模块哦 :) 它有一个JavaScript引擎,可以在(X)HTML文档的环境中执行JavaScript代码。
9
pyv8 是一组用于 V8 JavaScript 引擎的接口,这个引擎是 Google Chrome 浏览器使用的。
7
使用一个叫做 spidermonkey binding 的工具。
from spidermonkey import Runtime
rt = Runtime()
cx = rt.new_context()
result = cx.eval_script(whatyoupostedabove)